- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Strings
JavaScript Strings
JavaScript strings are used for storing and manipulating text.
A JavaScript string is zero or more characters written inside quotes.
Example
You can use single or double quotes:
Example
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:
Example
var answer2 = "He is called 'Johnny'";
var answer3 = 'He is called "Johnny"';
String Length
To find the length of a string, use the built-in length
property:
Example
var sln = txt.length;
Escape Character
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:
Example
The sequence \'
inserts a single quote in a string:
Example
The sequence \\
inserts a backslash in a string:
Example
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.
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:
Example
"Hello Dolly!";
You can also break up a code line within a text string with a single backslash:
Example
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:
Example
"Dolly!";
You cannot break up a code line with a backslash:
Example
"Hello Dolly!";
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");
Example
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:
Example
var y = new String("John");
// (x == y) is true because x and y have equal values
When using the ===
operator, equal values may not be equal, because the ===
operator expects equality in both data type and value.
Example
var y = new String("John");
// (x === y) is false because x and y have different types (string and object)
Or even worse. Objects cannot be compared:
Example
var y = new String("John");
// (x == y) is false because x and y are objects
Example
var y = new String("John");
// (x === y) is false because x and y are objects
(x==y)
and (x===y)
.Also note that comparing two JavaScript objects will always return
false
.
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP