What is the use of Single Quotes (”) & Double Quotes (“”) for Strings in JavaScript ?
In JavaScript, both single quotes (''
) and double quotes (""
) can be used to create strings, and they are interchangeable in most situations. However, there are a couple of differences worth noting:
- Quoting Inside Strings: If your string contains an apostrophe (single quote) and you use double quotes to define the string, you won’t need to escape the apostrophe, and vice versa.
let singleQuoted = 'He said, "Hello!"';
let doubleQuoted = "He said, 'Hello!'"; - Nested Quoting: If you need to use both types of quotes inside a string, you can alternate between them or use the backslash (
\
) as an escape character.let mixedQuotes1 = 'This is a string with "double quotes" inside.';
let mixedQuotes2 = "This is a string with 'single quotes' inside.";
let mixedQuotes3 = 'This is a string with \'single quotes\' and "double quotes" inside.';