JavaScript String

JavaScript strings are used to store and process text.

JavaScript String

Strings can store a series of characters, such as "John Doe."

a string can be any character inserted into quotation marks. you can use single or double quotes:


var carname = "Dodge Caliber";
var carname = 'Dodge Caliber';

you can use the index position to access each character in the string:


var character = carname[7];

The index of the string starts at 0, which means that the first character index value is .0, the second is .1, and so on.

You can use quotation marks in a string, and the quotation marks in a string should not be the same as those in a string:


var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';

you can also add escape characters to strings to use quotation marks:


var x = 'It\'s alright';
var y = "He is called \"Johnny\"";

 Try It! 

The Length of The String

you can use the built-in property length to calculate the length of the string:


var sln = txt.length;

 Try It! 

Special Characters

In JavaScript, strings are written in single or double quotes.

Because of this, javaScript cannot resolve the following instances:

 "We are the so-called "Vikings" from the north."

The string "We are the so-called" is truncated.

How to solve the above problems? You can escape the double quotes in the "Vikings" string using the backslash, as follows:

 "We are the so-called \"Vikings\" from the north."

A backslash '\' is an escape character. escape characters convert special characters to string characters:

Escaped characters can be used for escape apostrophes, line breaks, quotation marks, and other special characters.

The following table lists special characters that can be escaped in strings:

\"double quotes
\nline breaks
\ttab (tab)
\bthe backstag
\fpage breaks

A String Can be An Object

Typically, JavaScript strings are the original value and can be created using: var firstName = "Jake"

But we can also use the "new" keyword to define a string as an object: var firstName = new String("Jake")


var x = "Jake";
var y = new String("Jake");
typeof x // return String
typeof y // return Object

Do not create a String object. It slows down execution and can have other side effects:

 Try It! 


var x = "Jake";
var y = new String("Jake");
(x === y) // the result is false because x is a string and y is an object

 Try it! 

=== : It is absolutely equal, i.e. the data type and value must be equal.

String Properties and Methods

The original value string, such as "Jake," has no properties and methods (because they are not objects).

The original value can use the properties and methods of JavaScript because JavaScript can treat the original value as an object when executing methods and properties.

String methods are covered in the next section.

String Properties

constructorreturns the function that created the string property
lengthreturns the length of the string
prototypeallows you to add properties and methods to an object

String Methods

More examples of methods can be found in the: JavaScript String Object.

charAt()Returns the character at the specified index location
charCodeAt()Returns the Unicode value of the character at the specified index location
concat()Connect two or more strings and return the string after the connection
fromCharCode()Convert Unicode to a string
indexOf()Returns the location in the string where the specified character first appears
lastIndexOf()Returns the last place in the string where the specified character appeared
localeCompare()Compare the two strings in a locally specific order
match()A match was found for one or more regular expressions
replace()Replaces the substring that matches the regular expression
search()Retrieves the value that matches the regular expression
slice()Extracts fragments of the string and returns the extracted portion in the new string
split()Split the string into substring arrays
substr()Extracts the specified number of characters from the starting index number
substring()Extracts characters between two specified index numbers in the string
toLocaleLowerCase()Strings are converted to lowercase based on the host's local environment, and only a few languages, such as turkish, have locally specific case mappings
toLocaleUpperCase()Strings are converted to uppercase based on the host's local environment, and only a few languages, such as turkish, have locally specific case mappings
toLowerCase()Convert the string to lowercase
toString()Returns the string object value
toUpperCase()Convert the string to uppercase
trim()Remove the left and right white space(s) of the string
valueOf()Returns the original value of a string object