How to Convert Char to String in JavaScript ?
Last Updated :
09 Jul, 2024
In the article, we are going to learn about conversion Char to a string by using JavaScript, Converting a character to a string in JavaScript involves treating the character as a string, a character is a single symbol or letter, while a string is a sequence of characters. Strings can contain multiple characters, forming words or sentences.
There are several methods that can be used to Convert Char to String in JavaScript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using Concatenation
In this approach, we are using concatenation, converting a character to a string by appending it to an empty string (' ').
Syntax:
let result = char + ' ';
Example: In this example, we are using the above-explained approach.
JavaScript
// Orignal object
let char = ['A', 'B', 'C'];
console.log(typeof char);
// After using Concatenation
let result = char + '';
console.log(result);
console.log(typeof result);
Outputobject
A,B,C
string
Approach 2: Using String fromCharCode() Method
The JavaScript String.fromCharCode() method is used to create a string from the given sequence of UTF-16 code units. This method returns a string, not a string object.
Syntax:
String.fromCharCode( n1, n2, ..., nX )
Example: In this example we are using above-explained method to convert character into string.
JavaScript
let charCode = 100;
console.log(typeof charCode)
//using form.CharCode
let result = String.fromCharCode(charCode);
console.log(typeof result);
Approach 3: Using Join() Method
Using the join('') method on an array of characters like ['A', 'B', 'C'] creates a concatenated string result. The type changes from object to string.
Syntax:
array.join( separator )
Example: In this example we are using the above-explaine approach.
JavaScript
let data = ['A', 'B', 'C'];
console.log(typeof data)
let result = data.join('');
console.log(result)
console.log(typeof result);
Approach 4: Using ES6 Spread Operator
The ES6 spread operator spreads the character into an array, then joins it back into a string. By spreading the character, it creates an array with the character as its sole element, simplifying the conversion to a string.
Example:
JavaScript
const data = ['A', 'B', 'C'];
// Using ES6 spread operator
const result = [...data].join('');
console.log(result); // "ABC"
console.log(typeof result); // "string"
Approach 5: Using Template Literals
Template literals, introduced in ES6, allow for easy string interpolation and manipulation. We can use template literals to convert a character to a string by enclosing the character within backticks (`).
JavaScript
// Original character
let char = 'A';
// Using Template Literals
let result = `${char}`;
console.log(result); // Output: A
console.log(typeof result); // Output: string
Approach 6: Using toString Method
In this approach we convert an array of characters to a string directly using the toString method, although it will add commas, which we will remove with replace.
Example: Below example uses toString method to convert Char to String.
JavaScript
let chars = ['G', 'F', 'G'];
let str = chars.toString().replace(/,/g, '');
console.log(str);