Strings are the sequence of characters, symbols, and numbers. In JavaScript, the user can write strings either in ‘single quote‘ or in “double quote“. However, both print the same result on the terminal.
'neveropen' === "neveropen"
In some cases, users may need to print some part of the string with the quotes. For example, I’m, Hello “Geeks”, or It’s an apple, etc. In this situation, users have to use the single quote and double quote carefully. In this tutorial, the user will learn to write the string with quotation marks.
How simple string work in JavaScript?
As mentioned in the above section, the user gets the same output whether it writes the string in a single quote or double quote. Users can see the example code below.
Example: This example prints a simple string in the console.
Javascript
// String with double quotes let string1 = "Hello Geeks!!!" // String with single quotes let string2 = 'You are on neveropen!' console.log(string1) console.log(string2) |
Hello Geeks!!! You are on neveropen!
How user can add the quotes inside the string?
There are several ways to write a string with a quotation mark.
- Use the backslash (\) inside the string
- Using alternative quotes
Approach 1: Use the backslash (\) inside the string
One can use the backslash (\) inside the string to escape from the quotation mark. They will need to follow the below example to follow this method.
Syntax:
let string = "I\'m user of GeeksForGeeks."
Example: In this example, a string with a single quote is printed in the console.
Javascript
const string1 = "I\'m user of GeeksForGeeks." const string2 = 'I\ 'm user of GeeksForGeeks.' const string3 = 'Hello \'Geeks\'' const string4 = "Hello \'Geeks\'" console.log(string1) console.log(string2) console.log(string3) console.log(string4) |
I'm user of GeeksForGeeks. I'm user of GeeksForGeeks. Hello 'Geeks' Hello 'Geeks'
Approach 2: Using alternative quotes
One can write strings using alternative quotes. Users need to use single quotes inside the string if they have written the string in double quotes and vice-versa.
Syntax:
const string1 = "Hello 'Geeks'"
const strign2 = 'Hello "Geeks"'
Example: This example uses the above-explained approach to print a string with quotes in the console.
Javascript
const string1 = "I'm user of GeeksForGeeks." const string2 = 'I "m user of GeeksForGeeks.' const string3 = 'Hello " Geeks "' const string4 = " Hello 'Geeks'" console.log(string1) console.log(string2) console.log(string3) console.log(string4) |
I'm user of GeeksForGeeks. I"m user of GeeksForGeeks. Hello "Geeks" Hello 'Geeks'