MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Strings

Strings in JavaScript can be enclosed in Single quotes 'hello', Double quotes "Hello" and (from ES2015, ES6) in
Template Literals (backticks) `hello`.


var hello = "Hello";
var world = 'world';
var helloW = `Hello World`;

Strings can be created from other types using the String() function.


var intString = String(32); // "32"
var booleanString = String(true); // "true"
var nullString = String(null); // "null"

Or, toString() can be used to convert Numbers, Booleans or Objects to Strings


var intString = (5232).toString(); // "5232"
var booleanString = (false).toString(); // "false"
var objString = ({}).toString(); // "[object Object]"

Strings also can be created by using String.fromCharCode method


String.fromCharCode(104,101,108,108,111) //"hello"

Creating a String object using new keyword is allowed, but is not recommended as it behaves like Objects unlike primitive strings.


var objectString = new String("Yes, I am a String object");
typeof objectString;//"object"
typeof objectString.valueOf();//"string"

Concatenating Strings

String concatenation can be done with the + concatenation operator, or with the built-in concat() method on the
String object prototype.


var foo = "Foo";
var bar = "Bar";
console.log(foo + bar); // => "FooBar"
console.log(foo + " " + bar); // => "Foo Bar"
foo.concat(bar) // => "FooBar"
"a".concat("b", " ", "d") // => "ab d"

Strings can be concatenated with non-string variables but will type-convert the non-string variables into strings.


var string = "string";
var number = 32;
var boolean = true;
console.log(string + number + boolean); // "string32true"

String Templates

Strings can be created using template literals (backticks) `hello`


var greeting = `Hello`;

With template literals, you can do string interpolation using ${variable} inside template literals:


var place = `World`;
var greet = `Hello ${place}!`
console.log(greet); // "Hello World!"

You can use String.raw to get backslashes to be in the string without modification


`a\\b` // = a\b
String.raw`a\\b` // = a\\b


Conclusion

In this page (written and validated by ) you learned about Javascript Strings . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Reverse String.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.