How to usealternative quotes in Javascript
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 w3wiki." const string2 = 'I "m user of w3wiki.' const string3 = 'Hello " Geeks "' const string4 = " Hello 'Geeks'" console.log(string1) console.log(string2) console.log(string3) console.log(string4) |
I'm user of w3wiki. I"m user of w3wiki. Hello "Geeks" Hello 'Geeks'
JavaScript String with Quotes
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.
'w3wiki' === "w3wiki"
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 w3wiki!' console.log(string1) console.log(string2) |
Hello Geeks!!! You are on w3wiki!
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