STRINGS IN JAVASCRIPT

JS

A string in javascript is an unsigned 16-bit immutable sequence of elements. Each element in most cases represents a Unicode character. This rule does not always apply, I give an example of code where in the case of an emoji you need two bytes. ES2015 introduced literal templates. We can define a string through the backtick character. To define the backtick we use:


Alt Gr +
on Linux Alt+96 on Windows Alt +9 on Mac

Copy to Clipboard

Strings are used to store and manipulate text. A string consists of zero or more characters written in quotation marks.

let text = “Mario Rossi“;

You can use single or double quotation marks:

let carName1 = “Fiat 500“;

let carName2 = ‘Fiat 500‘;

You can use quotation marks within a string, as long as they do not match the quotation marks surrounding the string:

let answer1 = “It’s alright“;
let answer2 = “He is called ‘Johnny“;      //He is called ‘Johnny’
let answer3 = ‘He is called “Johnny” ;       //He is called “Johnny”

STRING LENGTH

To find the length of a string, use the property length embedded:

let text = “ABCDEFGHIJKLMNOPQRSTUVWXYZ“;
let length =text.length; //returns 26

ESCAPE CHARACTERS

Since strings must be written in quotes, JavaScript will misunderstand this string:

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

It will be cut off at “We are the so-called“. The solution to avoid this problem is to use the backslash escape character. Such a character (\) transforms special characters into string characters:

Escape

ESCAPE CHARACTERS

The sequence \” inserts a double quotation mark in a string:

let text = “We are the so-called \”Vikings\ from the north.“;      // We are the so-called “Vikings” from the north.

The sequence \’ inserts a single quotation mark in a string:

let text= ‘It\’s alright.‘; // It’s alright

The sequence \\ inserts a backslash into a string:

let text = “The character \\ is called backslash.“;                        // The character \ is called backslash.

escape characters
Copy to Clipboard

For better readability, programmers often prefer to avoid lines of code longer than 80 characters. If a JavaScript statement does not fit a line, the best place to break it is after an operator. You can also break a line of code within a text string with a single backslash:

document.getElementById(“demo”).innerHTML = Hello \
World! ;               //Hello World!

The method with \ is not the preferred method. It may not have universal support. Some browsers do not allow spaces behind the character \.
A safer way to break a string is to use string addition:

document.getElementById(“demo”).innerHTML = Hello ” +
“World!”               ; //Hello World!

JAVASCRIPT STRINGS AS OBJECTS

Normally, JavaScript strings are primitive values:

let x = “John“;

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

let y = new String(“John“);

When using the operator ==, x and y are equal example below.

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

When using the operator === , x and y are not equal example below.

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

(x == y) true or false?        \\false.

(x == y) true or false?        \\false.

NB: Comparing two JavaScript objects always returns false.

Copy to Clipboard

LINKS TO PREVIOUS POSTS

THE JAVASCRIPT LANGUAGE

LINK TO CODE ON GITHUB

GITHUB