Use ' or " for strings?

Hi,

As far as I understand you can use ’ or " for strings in JavaScript. What do you use? Do you always use the same or is it depending on something? What would you recommend me to use? I don’t want to internalize something wrong at the beginning.

Thanks :slight_smile:

You can, as you say, use either.

Personally, I delimit strings with "" because if I need an apostrophe I can do that without breaking the string or needing to escape:

console.log("I'm a string with an apostrophe in it!");

If I’d delimited with '', I’d need to do this:

console.log('I\'m a string with an escaped apostrophe in it!');

Which way you write it is personal preference, I suppose…

2 Likes

On top of quotation marks and apostrophes, you can also use backticks to designate a string, which allows you to use both apostrophes or quotation marks:

console.log(`Here is an apostrophe: ' and a quotation mark: " `)
2 Likes

Thank you very much. I understand it totally now :+1:

1 Like

Thank you too, also a helpful hint :+1:

1 Like