WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Strings


JavaScript strings are utilized for putting away and controlling text.


JavaScript Strings

A JavaScript string basically stores a progression of characters like "John Doe".

A string can be any content inside statements. You can utilize single or twofold quotes:

Example

var carname = "Volvo XC60";
var carname = 'Volvo XC60';
Try it Yourself »

You can utilize cites inside a string, as long as they don't coordinate the statements encompassing 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

The length of a string is found in the inherent property length:

Example

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

Special Characters

Because strings must be composed inside statements, JavaScript will misconstrue this string:

var y = "We are the supposed "Vikings" from the north."

The string will be hacked to "We are the purported ".

The answer for maintain a strategic distance from this issue, is to utilize the \ escape character.

The oblique punctuation line escape character transforms unique characters into string characters:

Example

var x = 'It\'s alright';
var y = "We are the purported \"Vikings\" from the north."
Try it Yourself »

The escape character (\) can likewise be utilized to embed other uncommon characters in a string.

This is the rundown of uncommon characters that can be added to a content string with the oblique punctuation line sign:

Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\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

Normally, JavaScript strings are crude qualities, made from literals: var firstName = "John"

But strings can likewise be characterized as items with the catchphrase new: 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 Properties and Methods

Primitive qualities, as "John Doe", can't have properties or then again strategies (since they are not objects).

But with JavaScript, techniques and properties are additionally accessible to crude qualities, in light of the fact that JavaScript treats crude qualities as articles when executing techniques and properties.

String strategies are shrouded in next chapter.


String Properties

Property Description
constructor Returns the capacity that made the String article's prototype
length Returns the length of a string
prototype Allows you to add properties and strategies to an object

String Methods

Method Description
charAt() Returns the character at the predefined list (position)
charCodeAt() Returns the Unicode of the character at the predefined index
concat() Joins at least two strings, and returns a duplicate of the joined strings
fromCharCode() Converts Unicode qualities to characters
indexOf() Returns the situation of the principal discovered event of a predefined esteem in a string
lastIndexOf() Returns the situation of the last discovered event of a predetermined esteem in a string
localeCompare() Compares two strings in the current locale
match() Searches a string for a match against a standard articulation, and returns the matches
replace() Searches a string for an esteem and restores another string with the esteem replaced
search() Searches a string for an esteem and restores the situation of the match
slice() Extracts a piece of a string and returns another string
split() Splits a string into a variety of substrings
substr() Extracts a piece of a string from a begin position through various characters
substring() Extracts a piece of a string between two indicated positions
toLocaleLowerCase() Converts a string to lowercase letters, as per the host's locale
toLocaleUpperCase() Converts a string to capitalized letters, as indicated by the host's locale
toLowerCase() Converts a string to lowercase letters
toString() Returns the estimation of a String object
toUpperCase() Converts a string to capitalized letters
trim() Removes whitespace from the two finishes of a string
valueOf() Returns the crude estimation of a String object