What's the difference?


#1

I have two things that are bugging me right now.

I’m not sure what the difference is between ‘===’ and '='
I know that === compares two values and = equates them, but both produce a true or false statement when used with if/else. I used them interchangeably and arrived at the same result.

var moonPhase = "full";
if(moonPhase === "full"){
  console.log("Howwwwlll!!");
} else{
  console.log("I swear I am not a werewolf...");
}
var moonPhase = "full";
if(moonPhase = "full"){
  console.log("Howwwwlll!!");
} else{
  console.log("I swear I am not a werewolf...");
}

I also noticed this with double quotes vs single quotes. My understanding is that double quotes are used when you are writing more than one character in a string, and single quotes for single characters. It seems single quotes are all you need. For example,
console.log(“I swear I am not a werewolf…”); works just as well as
console.log(‘I swear I am not a werewolf…’);


#2

i modified code your a little bit:

var moonPhase = "invalid";
if(moonPhase = "full"){
  console.log("Howwwwlll!!");
} else{
  console.log("I swear I am not a werewolf...");
}

now, when we defined a variable named moonPhase and set it to invalid, it will still log howwwwllll!!! to the console

that is because, a single equal signs assigns value to a variable. So if you do:

if(moonPhase = "full")

it will assign full to moonPhase variable, then evaluate if "full" is true. Which we can see by doing this:

if("full"){
   console.log(true);
}

while using === will check if value of variable and string are equal


#3

Not so, @shawhey. You can use double quotes for single character and vice versa.


var letter1 = 'A';     // single quotes
var letter2 = "B";     // double quotes
var sentence1 = 'This is sentence 1.';      // single quotes
var sentence2 = "This is sentence 2.";      // double quotes

All above are correct. The usage of double quotes versus single quotes are just preferences, both are universally correct to be used. You can google about the debates of each benefits, basically just choose and stick to one, it doesn’t matter.

The different of usage will be apparent only under certain circumstances. You will come to this situation, having an apostrophe or single quotation or double quotation in your string value, such as:

//Apostrophe in string
var response1 = 'I\'m fine';   // using single quotes, additional \ backslash needed
var response2 = "I'm fine"     // using double quotes

//Double quotes in string
var phrase1 = '"What is the time now?" I asked';     // using single quotes
var phrase2 = "\"What is the time now?\" I asked";   // using double quotes, additional \ backslash needed

//Single quotes in string
var order1 = 'When I say \'immediately,\' I mean some time before end of this month.';  // using single quotes, additional \ backslash needed
var order2 = "When I say 'immediately,' I mean some time before end of this month.";   // using double quotes

If you see from the above, you will have to use \ backslash to escape in order to tell the computer just ignore this quotes, that this is not the end of the syntax, proceed reading in order to avoid syntax error. So really, it depends on what you want to write in your code. Sometimes, one way is better than the other. Most of the time, it doesn’t matter.

It also matters in CC lessons, as their validation system is rather strict, if the lesson suggest single quotes, use it, just follow the instructions.

Hope this clarifies. :slight_smile:


Note:

If you have any question in future, it is also great to learn to use proper markdown to format your code in your post (use the </> icon to wrap your code), refer more here. It helps.


#4

Thank you for the clarification regarding quotation marks. You were very thorough! I also appreciate your advice on using proper markdown notation. I am always looking to improve my communication skills!
I am apparently still a bit unclear on the use of </> (triangle brackets?) to wrap code. It’s probably just me, but I did not find the “refer more here” link very helpful.

…so like below?

<var moonPhase = “full”;
if(moonPhase === “full”){
console.log(“Howwwwlll!!”);
} else{
console.log(“I swear I am not a werewolf…”);
}>


#6

@shawhey, you’re welcome. :slight_smile:

The </> is not something you type, it is an icon inside the editor when you’re typing a post or reply.

Just press that icon </> once. And you will see something like below:

```
type or paste code here
```

//Delete the line above and paste your code inside those triple backticks.

Or else you can hightlight all your code, all of those lines, then click </> once. You will see your code formatted nicely.

You can try this with editing your post above this, delete the < in front of var moonPhase and > at the end of code. Highlight it all together, then find the icon </>, and click once. The icon is at the top of your editor, 6th from the left, you can see a bubble icon, Bold icon, Italic icon… the </> icon is around there.

Actually, if you can’t find the Icon, you only need to type triple backticks ``` then follow with your code and end it with triple backticks ` `` again. Using the icon is just another convenient way if people can’t seem to find backticks in their keyboard.


About the question regarding double quotes vs single quotes, I found one here an interesting read which cover different perspectives (it depends on how you’re using them):