I have just begun learning JS at W3 Schools.
At the Introduction(https://www.w3schools.com/js/js_intro.asp), it is stated that JS is insensitive to the use of either single- or double quotes.
The Introduction states these two lines are the same:
It may also be worth noting beore you get too deep into your learning that the examples there may not be up to date.
It is considered best to avoid things like in-line onclick attributes and instead use event listeners separate to the HTML.
While W3Schools is very popular and always comes up in search, it’s not the best maintained in terms of keeping current.
Thank you; that is indeed the code that works. I’m however a bit puzzled as why W3 states JS is indifferent to double- and single quotes, while this is evidently not true, demonstrated by their own examples provided at that.
Thank you SamA74; I feared as much. What would be a more relevant tutorial to follow? I know JS has gone through quite a few revolutions, such as var having become rather outdated and let being its contemporary replacement. Quite a few books I got hold of still use var a lot, and even as a neophyte I seemed to notice that something was off here.
If you need to put code within quotes then it does not matter whether you use single or double quotes. However if you start with a single then you must finish with single and if you start with a double then you must finish with a double. As demonstrated in my code it can be very convenient to use both single and double so you can nest: the whole onclick code is within double quotes and the ID and filename are within single quotes.
Just to clarify a point I didn’t see mentioned yet.
onclick=""
The above is an html attribute. You get a JavaScript error because the html parser closes the attribute at the next matching quote it sees and the Js that is trying to run is effectively this:
JS is indifferent. The problem here is that you have tried to nest double quotes within double quotes and there are few (if any) programming languages that permit that. The same is true for single quotes.
I find that style guides tend to settle on a preference of using double quotes.
There are several reasons for that. One is that apostrophes don’t need to be escaped when using double quotes. Another is that you are not supposed to embed HTML code inside of your JavaScript code, so standardising on using double quotes in JavaScript for text, and double quotes in HTML for attributes, helps to encourage that separation.
Because though there are good arguments on both sides of the discussion, choose a single style guide to use.
Until you settle on a style guide for you and your team to use you will instead be wasting time reformatting others’ code to fit your own personal style, that will only be reformatted once again when someone with different standards comes to it.
Ensure that everybody on your team is using that same style guide and you will be free of arguments about which type of quote, or how many spaces or tabs to use. When you are all working from the same style guide, you can stop wasting time, and can actually get on with focusing on the coding problem at hand.