Sunday, November 18, 2018

JavaScript Strings

JavaScript Strings

A JavaScript string is zero or more characters written inside quotes.

var x = "John Doe";

You can use single or double quotes:
var carname = "Volvo XC60";  // Double quotesvar carname = 'Volvo XC60';  // Single quotes

String Length

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

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;

Special Characters

Because strings must be written within quotes, JavaScript will misunderstand this string:

var x = "We are the so-called "Vikings" from the north.";

Strings Can be Objects

Normally, JavaScript strings are primitive values, created from literals:
var firstName = "John";
But strings can also be defined as objects with the keyword new:
var firstName = new String("John");

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

// typeof x will return string// typeof y will return object

























Share:

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *

Popular Posts

Blog Archive

Blog Archive

Hassan.mosmer1@gmail.com