Spaces, semi-colons - do they matter?


#1

Just on 20. Math and the modulo.
I tried answering the question with different spacing and with a missing semi-colon, but all lines of code worked...

// Below is an example of printing the remainder of 18/4 using modulo:
// console.log(18 % 4);
console.log(14 % 3);
console.log (99%8) ;
console.log(11%3)

Are spaces unimportant? Do you need semi-colons?
Thank you!


#2

Spaces aren't necessarily important no. To the console they're white spaces meant for nothing. For us they're meant to organize the code to look better.

JavaScript is a language that DOES NOT REQUIRE semi-colons. But it is best practice to always use them.

function hello(){
  if ("Greeting" ==="Greeting") {
    return "Sup"
  }
}

Technically this could work. The problem about not using semi-colons is the console will get confused about when a statement ends, and when a new one begins.

Best practice is to utilize the semi-colon whenever it won't interfere with the code. Simply due to the headache it will cause otherwise.


#3

Hmm, Maybe I should simplify this because you're still on the first lesson!

console.log(11 % 3)

Will work as long as you don't have any code after it that the console could assume is on the same line. I don't have any examples of what those could be, but rest assured you shouldn't run into that situation if you're utilizing your semi-colons properly!


#4

Semi-colons matter. That is all I have to say about that. Just try putting all the above code on one line without them, and see. As for spaces. it depends on compression or readability. It makes no sense to make compressed code deliberately readable, so it follows to make no sense compressing readable code. Let the operators stand out in uncompressed code.

console.log( 11 % 3 );

Written for readability, not execution or download efficiency. We ought to use white space as semantically as possible in source code, so the meaning and purpose read through at a glance. It's what readability is all about.