JavaScript Strings

Javascript string is used for storing text data and manipulate it.

Strings

A string is a continuous series of characters like “This is a cat”.
String data type are always within either single quotes or double quotes.

var modalMake = "BMW 160";   // Using double quotes
var modalMake = 'BMW 160';   // Using single quotes

A string can contain quotes within it provided it is not same as surrounding quotes of string.

var answer = "Hi O'conol";                   // Single quote inside double quotes
var answer = "It is known as 'famous place'";    // Single quotes inside double quotes
var answer = 'It is known as "famous place"';    // Double quotes inside single quotes

String Length

The length of a string can be counted using the in built String length property

<!DOCTYPE html>
<html>
<body>

String length is <span id="demo"></span> characters.

<script>
var str = "The quick brown fox jumps over the lazy dog";
var strlen = str.length;
document.getElementById('demo').innerHTML=strlen;
</script>


</body>
</html>

Download the code Run the code

Special Characters

As noted above that string values are written within quotes, JavaScript throws an error if strings written as below

 var str='Kathleen D'Souza';

In above statement, variable str value is set to “Kathleen D”

To fix this problem, need to escape character using \

<!DOCTYPE html>
<html>
<body>

Quote within string : <span id="demo"></span>

<script>
var str='Kathleen D\'Souza';
document.getElementById('demo').innerHTML=str;
</script>


</body>
</html>

Download the code Run the code

There are other characters which need to be escape in a string. Below is a list of such characters

Code    Outputs
\'      single quote
\"      double quote
\\      backslash
\n      new line
\r      carriage return
\t      tab
\b      backspace
\f      form feed

Break Long statements in multiple lines

Generally programmers avoid lines with more than 80 characters due to readability

If JavaScript statement is more than 80 characters long, it is good practice to break a statement after an operator sign

<!DOCTYPE html>
<html>
<body>

<span id="element"></span>

<script>
document.getElementById('element').innerHTML =
"The total of 9 + 3 is 12";
</script>


</body>
</html>

Download the code Run the code

It is also possible to break lines from within string text like below

<!DOCTYPE html>
<html>
<body>

<span id="element"></span>

<script>
document.getElementById('element').innerHTML = "The total of 9 + 3 is : \
 12";
</script>


</body>
</html>

Download the code Run the code

You can also use string concatenation to break lines from within string text like below

<!DOCTYPE html>
<html>
<body>

<span id="element"></span>

<script>
document.getElementById('element').innerHTML = "The total of 9 + 3 is :" +
"12";
</script>


</body>
</html>

Download the code Run the code

Strings Can be of type Objects

A string can be of type object if it is created using new String(text) method

var modalMake = "BMW 160"; 
var modalMake1 = new String("BMW 160");

//type of modalMake is string
//type of modalMake is object

modalMake === modalMake1 will returns false as it will check for variable type also for comparison.
However modalMake == modalMake1 will returns true as both variable contains same string value.

<<< JavaScript EventsJavaScript String Methods >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .