/    /  Javascript-Strings

Javascript-Strings

 

The JavaScript string is an object that represents a sequence of characters and used for storing and manipulating text.

 

Example:

 

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Don't match the quotes surrounding the string.</p>
<p id="demo"></p>
<script>
var answer1 = "It's javascript";
var answer2 = "welcome 'i2tutorials'";
var answer3 = 'Best "Good"';
document.getElementById("demo").innerHTML =
answer1 + "<br>" + answer2 + "<br>" + answer3;
</script>
</body>
</html>

 

OUTPUT:

 

JavaScript Strings

Don’t match the quotes surrounding the string.

 

It’s javascript

welcome ‘i2tutorials’

Best “Good”

 

String Length:

 

Find the length of a string, use the built-in length property:

 

Example:

 

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Properties</h2>

<p>Length of a string:</p>

<p id="demo"></p>

<script>
var txt = "Welcome to I2 tutorials";
var sln = txt.length;
document.getElementById("demo").innerHTML = sln;
</script>

</body>
</html>

 

OUTPUT:

 

JavaScript String Properties

Length of a string:

 

23

 

Escape Character:

 

escape character turns special characters into string characters:

 

Code                           Result                          Description

 

\’                                  ‘                                   Single quote

\”                                 ”                                   Double quote

\\                                 \                                   Backslash

 

Example:

 

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Strings</h2>

<p> \' inserts a single quote in a string.</p>

<p id="demo"></p>

<script>
var x = 'It\'s alright.';
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

 

OUTPUT:

 

JavaScript Strings

\’ inserts a single quote in a string.

 

It’s alright.

 

escape sequences are valid in JavaScript:

 

Code   Result

 

\b         Backspace

\f         Form Feed

\n         New Line

\r         Carriage Return

\t         Horizontal Tabulator

\v         Vertical Tabulator

 

Breaking Long Code Lines:

 

Statement does not fit on one line, the best place to break it is after an operator:

 

Example:

 

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Statements</h2>

<p>
break a code line is after an operator or a comma.
</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"Welcome I2tutorials!";
</script>

</body>
</html>

 

OUTPUT:

 

JavaScript Statements

break a code line is after an operator or a comma.

 

Welcome I2tutorials!

 

Strings Can be Objects:

 

Strings are primitive values, created from literals.

 

var firstName = "hello";

 

Strings defined as objects with the keyword new:

 

var firstName = new String("hello");

 

Example:

 

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>

<script>
var x = "hello";        // x is a string
var y = new String("hello");  // y is an object

document.getElementById("demo").innerHTML =
typeof x + "<br>" + typeof y;
</script>

</body>
</html>

 

OUTPUT:

 

string

object