JavaScript Strings

JavaScript strings
\n inside the string is used for ‘new line’ and \t is used for ‘tab’.

Once a string has been defined in JavaScript it’s value can not change. To get around this, when you use operators to change the string, a new string is created and the old string is destroyed, all behind the scenes.

Use the toString() method to convert a value to a string. This method can be passed the radix to convert the number to another base number system.

var x = 10;
x.toString(); //"10"
x.toString(2); //"1010"
x.toString(8); //"12"
x.toString(10); //"10"
x.toString(16); //"a"

Use the String() casting function to covert values if it’s possible for the value to be null or undefined This function will use the toString() method if the variable has one, but will show null or undefined if not.

Using the + operator to add "" (the empty string) to a value will also convert the value to a string:

var c = 10; //10
c = c + “”; //”10″

Leave a Comment

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

Scroll to Top