Skip to main content

The String data type in JavaScript. Introduction to String, Math and Date

var str5= new String(“hello”);  // This is a string object
console.log(str4); // This will return the string ‘hello’

The string data type in JavaScript can be any group of characters enclosed by a single or double-quotes or by backticks.

 

var str1 = “This is a string1”;  // This is a string primitive type or string literal
var str2= ‘This is a string2’;
var str3 = `This is a string3`;

 

Alternatively, we can use the String() function to create a new string.

var str4 = String(‘hi’);  // This creates a string literal with value ‘hi’

The String() function is also used to convert a non-string value to a string.

String(4); // This statement will create a string ‘4’

Like the ‘number’ and the ‘boolean’ data types, a ‘String’ object can be created using the ‘new’ operator: