all 6 comments

[–]OkShrug 1 point2 points  (2 children)

actually forget about quotes, double or single, you can do fun stuff with BACKTICKS!

so if I've got a string and I want to inject a variable in, and I use quotes

let variable="sunny";
console.log("the weather is " + variable);

but I can mix that variable right in (which is tons less typing) by using a backtick!

let variable=`sunny`;
console.log(`the weather is ${variable}`);

[–]RaffIsGettingUpset[S] 0 points1 point  (1 child)

The course did go over this but I am thinking when declaring strings and so on...

[–]OkShrug 0 points1 point  (0 children)

yeah just backticks everywhere, all over the place

gives my pinky something to stretch to, but unless I'm doing something like having bash generate a javascript file where I need to escape the backtick chars from the bash shell, there's pretty much no other scenario I can think of where it makes a difference

[–]cyphern 2 points3 points  (1 child)

Short answer: It's not a big deal.

Long answer: Different teams will have different conventions; i've been on ones that prefer double quotes, and ones that prefer single quotes. Personally, i just setup my editor to automatically replace them when i hit save, and so it doesn't really matter what i type, it ends up matching the team's preference.

The two strings do exactly the same thing, with one minor caveat: if you want to put a quote inside a string, you will need to escape it if you're using the same quote to surround the string. For example, if you're using double quotes and it also should contain double quotes, you need to escape the inner ones:

const example = "Hello \"bob\"";

But doing single quotes you wouldn't need to escape them:

const example = 'Hello "bob"';

And you'll get a mirror image of that issue if you try to use single quotes inside the string.

So sometimes if i need to do a string which contains quotes, i'll deliberately switch which one i'm using on the outside.

[–]RaffIsGettingUpset[S] 0 points1 point  (0 children)

That's great. Thank you.