Warning: session_start(): open(/tmp/sess_35a9637dd0a0d63f871b64e63b8be4ac, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Convert Array To String Without Commas In Javascript - LearnShareIT

How To Convert Array To String Without Commas In Javascript

Convert Array to String without Commas in JavaScript

To convert array to string without commas in Javascript, we can use loops, operators, and built-in methods or combine methods. Follow the article below to learn how to do it.

Convert array to string without commas in Javascript

Use (for) loop and + operator

The loop is handy for arrays and helps solve most small and large problems. 

Today I will combine with for loop and + operator to convert array to string without commas in JavaScript. The + operator is basic, but to fully understand it, we must learn about it thoroughly.

Example:

// Create function Convert Array to String without commas
function convertArray(array) {
	var result = "";
	for (let index = 0; index < array.length; index++) {
		result = result + array[index];
	}
	return result;
}

var newArray1 = ['Convert', 'array', 'to', 'string'];
var newArray2 = [1, 2, 3, 4, 5];

// Show the results
console.log(convertArray(newArray1));
console.log(convertArray(newArray2));

Output

Convertarraytostring
12345

In the above example, we create a variable result to store the result and initialize it to an empty string (which is forced to initialize and is an empty string) to store the result. When accessing the elements of the array one by one, we use the + operator to join the elements together. And the result is a string.

I will show you why the variable result must be initialized to an empty string.

Example: 

// If the variable "result" is not initialized, the default value is undefined
var result; 
console.log(result + 1);

Output

NaN

Example:

// If the variable "result" is not initialized, the default value is undefined
var result; 
console.log(result + 'Today');

Output

undefinedToday

Results like the example above will affect the desired result.

Why is there such a result? You must learn carefully about this + operator.

Use join() method

The Join() method is used to concatenate the elements of an array into a string, separated by a user-entered character. The default character will be commas.

Syntax:

array.join(separator)

Parameter:

  • separator: is the character that separates elements from each other, default is comma ‘,’

The function will return a string and will not affect the old array.

Example:

// Initialize and assign values to arrays
var newArray = ['There','are', 3, 'rabbits'];
console.log(newArray.join(''));

Output

Thereare3rabbits

With this method, it helps us to do many things

Combine two methods toString() and replaceAll()

How do we combine these two methods?

  • First, use toString(); it will return a string whose value is a comma-separated array ‘,’
  • Then use ReplaceAll() to replace the comma with an empty string ”.

toString() method

The toString() method will return a string with the value of a comma-separated array ‘,’ and it also does not affect the original value.

Syntax:

array.toString();

Parameter: None

replaceAll() method

The replaceAll() method returns a new string with all matches of a pattern replaced with a replacement string.

Syntax:

replaceAll(pattern, replacement)

Parameters:

  • pattern: a substring or a regular expression
  • replacement: can be a string or a function to replace a pattern

Combine two methods to convert array to string without commas in Javascript.

Example:

// Create the variable "result" to save the result
var result;

// Create array "newArray" and initialize it with values
var newArray = ['A', 'day', 'has', '24', 'hours'];
var convertToString = newArray.toString();

// Use replaceAll() method
result = convertToString.replaceAll(',', '');

// Show the result
console.log(result);

Output

Adayhas24hours

Using the toString() method to convert an array to a string, its return value is a comma-separated array ‘,’. After we continue to use the replaceAll(',', '') method with two parameters as above, let’s replace comma ‘,’ with non-delimiter ” to convert array to string without commas in Javascript.

Summary

In this article, we have shown you 3 ways to convert array to string without commas in Javascript. They are very simple, easy to understand, and fast. You can use our method or get as creative as you like. We hope this article is helpful to you.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *