Javascript Strings Flashcards
JavaScript Strings
JavaScript strings are used for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
var x = “John Doe”; //String written inside quotes
You can use single or double quotes: var carName1 = "Volvo XC60"; // Double quotes var carName2 = 'Volvo XC60'; // Single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string: var answer1 = "It's alright"; var answer2 = "He is called 'Johnny'"; var answer3 = 'He is called "Johnny"';
var x = "John Doe"; // String written inside quotes document.getElementById("demo").innerHTML = x;
next
var carName1 = "Volvo XC60"; // Double quotes var carName2 = 'Volvo XC60'; // Single quotes
document.getElementById(“demo”).innerHTML =
carName1 + “ “ + carName2;
next
var answer1 = "It's alright"; var answer2 = "He is called 'Johnny'"; var answer3 = 'He is called "Johnny"';
document.getElementById(“demo”).innerHTML =
answer1 + “br>” + answer2 + “br>” + answer3;
String Length
To find the length of a string, use the built-in length property:
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length;
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; document.getElementById("demo").innerHTML = 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.”;
The string will be chopped to "We are the so-called ". The solution to avoid this problem, is to use the backslash escape character. The backslash (\) escape character turns special characters into string characters:
Code Result Description
' ‘ Single quote
" “ Double quote
\ \ Backslash
The sequence \" inserts a double quote in a string: var x = "We are the so-called \"Vikings\" from the north.";
The sequence ' inserts a single quote in a string:
var x = ‘It's alright.’;
etc etc.
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
The 6 escape characters above were originally designed to control typewriters, teletypes, and fax machines. They do not make any sense in HTML.
var x = "We are the so-called \"Vikings\" from the north."; document.getElementById("demo").innerHTML = x;
displays: var x = "We are the so-called \"Vikings\" from the north."; document.getElementById("demo").innerHTML = x;
Breaking Long Code Lines
For best readability, programmers often like to avoid code lines longer than 80 characters.
If a JavaScript statement does not fit on one line, the best place to break it is after an operator: Hello Dolly
You can also break up a code line within a text string with a single backslash “": Hello Dolly
The \ method is not the preferred method. It might not have universal support.
Some browsers do not allow spaces behind the \ character.
A safer way to break up a string, is to use string addition “+”: Hello Dolly
You cannot break up a code line with a backslash “":
Hello Dolly
The best place to break a code line is after an operator or a comma:
document.getElementById(“demo”).innerHTML =
“Hello Dolly!”;
next
document.getElementById(“demo”).innerHTML = “Hello \
Dolly!”;
next
A safer way to break up a string, is to use string addition:
document.getElementById(“demo”).innerHTML = “Hello “ +
“Dolly!”;
next
document.getElementById(“demo”).innerHTML = \
“Hello Dolly!”;
Strings Can be Objects
Normally, JavaScript strings are primitive values, created from literals:
var x = “John”;
But strings can also be defined as objects with the keyword new:
var y = new String(“John”);
// typeof x will return string // typeof y will return object
*Don’t create strings as objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:
When using the == operator, equal strings are equal: // (x == y) is true because x and y have equal values
When using the === operator, equal strings are not equal, because the === operator expects equality in both type and value. // (x === y) is false because x and y have different types (string and object)
Or even worse. Objects cannot be compared: // (x == y) is false because x and y are different objects
last ex: var x = new String("John"); var y = new String("John");
// (x === y) is false because x and y are different objects
Note the difference between (x==y) and (x===y).
Comparing two JavaScript objects will always return false.
var x = "John"; // x is a string var y = new String("John"); // y is an object
document.getElementById(“demo”).innerHTML =
typeof x + “br>” + typeof y;
displays:
string
object
next
var x = "John"; // x is a string var y = new String("John"); // y is an object document.getElementById("demo").innerHTML = (x==y);
displays:
Never Create Strings as objects.
Strings and objects cannot be safely compared.
true
//This is false because one is an object now while the other is a string//
next
var x = "John"; // x is a string var y = new String("John"); // y is an object document.getElementById("demo").innerHTML = (x===y);
displays:
Strings and objects cannot be safely compared.
false
next
var x = new String("John"); // x is an object var y = new String("John"); // y is an object document.getElementById("demo").innerHTML = (x==y);
displays:
JavaScript objects cannot be compared.
false