WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Strings


JavaScript strings are used to store and process text.


JavaScript string

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

The string can be any character inserted in quotes. You can use single or double quotes:

Example

var carname = "Volvo XC60";
var carname = 'Volvo XC60';
Try it Yourself »
You can use the index position to access each character in the string:

Example

var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is designated "Johnny"';
Try it Yourself »

String Length

You can use the built-in attribute length to calculate the length of a string:

Example

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
Try it Yourself »

Special Characters

In JavaScript, strings are written in single or double quotes. Because of this, the following example JavaScript cannot be parsed:
var y = "We are the supposed "Vikings" from the north."
The string "We are the so-called" is truncated. How to solve the above problems? You can use backslashes (\) to escape double quotes in the "Vikings" string, as follows:

Example

var x = 'It\'s alright';
var y = "We are the purported \"Vikings\" from the north."
Try it Yourself »
The backslash is an escape character. Escape characters convert special characters to string characters: The escape character (\) can be used to escape apostrophes, newlines, quotes, and other special characters. The following table lists special characters that can be escaped using escape characters in strings:
Code Output
\ ' Single quotes
\ " Double quotes
\\ backslash
\ n line feed
\ r Enter
\ t tab
\ b Backspace character
\ f Form feed

Breaking Long Code Lines

For best comprehensibility, software engineers regularly prefer to maintain a strategic distance from code lines longer than 80 characters.

If a JavaScript articulation does not fit on one line, the best spot to break it is after an operator:

Example

document.getElementById("demo").innerHTML =
"Hello Dolly.";
Try it Yourself »

You can likewise separate a code line within a content string with a solitary backslash:

Example

document.getElementById("demo").innerHTML = "Hello \
Dolly!";
Try it Yourself »

The most secure (yet a little slower) approach to break a long string is to utilize string addition:

Example

document.getElementById("demo").innerHTML = "Hello" +
"Dolly!";
Try it Yourself »

You can't separate a code line with a backslash:

Example

document.getElementById("demo").innerHTML = \
"Hello Dolly!";
Try it Yourself »

Strings Can be Objects

Usually, JavaScript strings are primitive values and can be created using characters: var firstName = "John"

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

Example

var x = "John";
var y = new String("John");

/typeof x will return string
//typeof y will return object
Try it Yourself »

When utilizing the == equity administrator, break even with strings looks equal:

Example

var x = "John";             
var y = new String("John");

/(x == y) is genuine in light of the fact that x and y have break even with qualities
Try it Yourself »

When utilizing the === correspondence administrator, measure up to strings are not equivalent, on the grounds that the === administrator anticipates equity in both kind and value.

Example

var x = "John";             
var y = new String("John");

/(x === y) is false on the grounds that x and y have diverse sorts (string and item)
Try it Yourself »

Or much more terrible. Articles can't be compared:

Example

var x = new String("John");             
var y = new String("John");

/(x == y) is false on the grounds that x and y are diverse objects
//(x == x) is genuine in light of the fact that both are the equivalent object
Try it Yourself »

String attributes and methods

Primitive strings, such as "John", have no properties or methods (because they are not objects).

Raw values ​​can use JavaScript properties and methods because JavaScript can treat raw values ​​as objects when executing methods and properties.

The string method is described in the next section.


String attributes

Attribute Description
constructor Returns a function that creates a string attribute
length Returns the length of the string
prototype Allows you to add attributes and methods to objects


String method

Method Description
charAt () Returns the character at the specified index position
charCodeAt () Returns the Unicode value of the character at the specified index position
concat () Concatenate two or more strings and return the connected string
fromCharCode () Convert Unicode to string
indexOf () Retrieves the first occurrence of the specified character in the string
lastIndexOf () Retrieves the last occurrence of the specified character in the string
localeCompare () Compare two strings in a local specific order
match () Find one or more regular expression matches
replace () Replace substrings that match the regular expression
search () Retrieve a value that matches the regular expression
slice () Extract a fragment of a string and return the extracted part in a new string
split () Split a string into an array of substrings
substr () Extract the specified number of characters from the string from the starting index number
substring () Extract characters between two specified index numbers in a string
toLocaleLowerCase () Convert strings to lowercase according to the locale of the host. Only a few languages ​​(such as Turkish) have locale-specific case mappings.
toLocaleUpperCase () Convert strings to uppercase according to the locale of the host. Only a few languages ​​(such as Turkish) have locale-specific case mappings.
toLowerCase () Convert string to lowercase
toString () Returns a string object value
toUpperCase () Convert string to uppercase
trim () Remove white space at the beginning and end of the string
valueOf () Returns the original value of a string object