Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

JavaScript Strings

Uploaded by

Maiwand Mangal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

JavaScript Strings

Uploaded by

Maiwand Mangal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

JavaScript Strings
❮ Previous Next ❯

Strings are for storing text

Strings are written with quotes

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

Example

let text = "John Doe";

Try it Yourself »

You can use single or double quotes:

Example
let carName1 = "Volvo XC60"; // Double quotes
let carName2 = 'Volvo XC60'; // Single quotes
Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

Note
Strings created with single or double quotes works the same.

There is no difference between the two.

Quotes Inside Quotes


You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';

Try it Yourself »

Template Strings
Templates were introduced with ES6 (JavaScript 2016).

Templates are strings enclosed in backticks (`This is a template string`).

Templates allow single and double quotes inside a string:

Example

let text = `He's often called "Johnny"`;


Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

Note
Templates are not supported in Internet Explorer.

String Length
To find the length of a string, use the built-in length property:

Example

let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


let length = text.length;

Try it Yourself »

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

let text = "We are the so-called "Vikings" from the north.";

The string will be chopped to "We are the so-called ".

To solve this problem, you can use an backslash escape character.

The backslash escape character ( \ ) turns special characters into string characters:

Code Result Description


\' Tutorials 
'
Exercises  Services 
Single quote
 Sign Up Log in
\" " Double quote
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
\\ \ Backslash

Examples
\" inserts a double quote in a string:

let text = "We are the so-called \"Vikings\" from the north.";

Try it Yourself »

\' inserts a single quote in a string:

let text= 'It\'s alright.';

Try it Yourself »

\\ inserts a backslash in a string:

let text = "The character \\ is called backslash.";

Try it Yourself »

Six other 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
NoteTutorials 
 Exercises  Services   Sign Up Log in

HTML CSS characters


The 6 escape JAVASCRIPT SQL
above were PYTHON
originally JAVA
designed to PHP HOW
control typewriters, TO
teletypes, W3.CSS
and C
fax machines. C++
They C
do not make any sense in HTML.

Breaking Long Lines


For readability, programmers often like to avoid long code lines.

A safe way to break up a statement is after an operator:

Example
document.getElementById("demo").innerHTML =
"Hello Dolly!";

Try it Yourself »

A safe way to break up a string is by using string addition:

Example
document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";

Try it Yourself »

Template Strings
Templates were introduced with ES6 (JavaScript 2016).

Templates are strings enclosed in backticks (`This is a template string`).


Templates allow multiline strings:
 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
Example
let text =
`The quick
brown fox
jumps over
the lazy dog`;

Try it Yourself »

Note
Templates are not supported in Internet Explorer.

JavaScript Strings as Objects


Normally, JavaScript strings are primitive values, created from literals:

let x = "John";

But strings can also be defined as objects with the keyword new :

let y = new String("John");

Example
let x = "John";
let y = new String("John");
Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

Do not create Strings objects.

The new keyword complicates the code and slows down execution speed.

String objects can produce unexpected results:

When using the == operator, x and y are equal:

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

Try it Yourself »

When using the === operator, x and y are not equal:

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

Try it Yourself »

Note the difference between (x==y) and (x===y) .

(x == y) true or false?

let x = new String("John");


let y = new String("John");

Try it Yourself »

(x ===Tutorials Exercises 
y) trueor false? Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C
let x = new String("John");
let y = new String("John");

Try it Yourself »

Comparing two JavaScript objects always returns false.

Complete String Reference


For a complete String reference, go to our:

Complete JavaScript String Reference.

The reference contains descriptions and examples of all string properties and methods.

Test Yourself With Exercises

Exercise:
Use the length property to alert the length of txt .

let txt = "Hello World!";


let x = ;
alert(x);

Submit Answer »
Start the Exercise
 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C

❮ Previous Next ❯

W3schools Pathfinder
Track your progress - it's free! Sign Up Log in

COLOR PICKER

 

 SPACES UPGRADE AD-FREE NEWSLETTER

GET CERTIFIED CONTACT US

Top Tutorials Top References


HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference

 How To Tutorial
Tutorials 
SQL Tutorial
Exercises  Services  SQL Reference

Python Reference
Sign Up Log in
Python Tutorial W3.CSS Reference
HTML
 CSS W3.CSS Tutorial
JAVASCRIPT SQL PYTHON Bootstrap
JAVA Reference
PHP HOW TO W3.CSS C C++ C
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT CLASSROOM


W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy
policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

You might also like