var name = “John said “Hi there developers” and waved”;

What do you think would happen if we tried to execute this statement? Give it a go and find out for yourself. Head over to your Google Chrome browser, enter the code above and see what happens.

Did you get a nice big error message about unexpected characters? Of course you did – that’s because JavaScript doesn’t know where your string ends. To JavaScript, it sees you are trying to assign a variable called ‘name’ with the value ‘John said’ then it sees some ‘random’ text ‘Hi there developers’. However, JavaScript doesn’t know that this is still part of your string as it saw a closing quotation mark, so assumed your string was finished. It doesn’t know how to handle ‘Hi there developers’, so it informs you that it has encountered an unexpected character.

Hopefully you can see why JavaScript would get confused by this statement. How can it know the difference between your quote and the ending of your string? Well, this is why it’s so useful to be able to assign strings with both single and double quotes. In JavaScript you can include quotes inside the string as long as they don’t match the enclosing quotes, so if you use double quotes on the outside, you must use single quotes on the inside, and vice versa. Let’s see some examples to illustrate this better: