JavaScript Strings JAVASCRIPT
- JavaScript Introduction
- JavaScript Syntax
- JavaScript innerHTML
- JavaScript document.write()
- Javascript - window.alert()
- JavaScript - console.log()
- JavaScript Comments
- JavaScript Variables
- JavaScript Operators
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- 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 Conditions
- JavaScript Switch
- JavaScript Loop For
- JavaScript While Loop
- JavaScript Break and Continue
- JavaScript Type Conversion
- JavaScript Errors
- JavaScript Scope
- JavaScript this Keyword
- JavaScript Classes
- JavaScript Debugging
- JavaScript - Changing CSS
- JavaScript JSON
JavaScript Strings
JavaScript Strings
A JavaScript string is zero or more characters written inside quotes. You can use single or 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 answer2 = "He is called 'Deepak'";
var answer3 = 'He is called "Deepak"';
String Length
To find the length of a string, use the built-in length
property:
Escape Character
Because strings must be written within quotes, JavaScript will misunderstand this string:
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 or single quote in a string:
Example
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.';
The sequence
\\
inserts a backslash in a string:var x = "The character \\ is called backslash.";