This post is intended to be a handy reference for people who are new to learning Javascript, and probably new to programming in general.
It’s not meant be an exhaustive reference of the language by any means, there are plenty of those around, MDN being one of my favorites, but just a simple overview of what I consider to be some of the most essential things a beginner should aim to have under their belt before they attempt to tackle more advanced things.
There are just a few things to get you started for now but I will add to the list in time.
I encourage anyone starting out in Javascript to be as familiar with these aspects of the language as they can and to practice typing them out on a daily basis until they are well and truly memorised. I feel strongly that memorising the syntax, even if by rote at first, can aid in one’s progress as it forces one to think of why things are supposed to go where they do and this assists in understanding the purpose of each of the individual language features.
Make it your goal to be able to close your eyes and picture a loop, or a function or a switch statement for example, and have the confidence to know that you could type anyone of them out on demand without needing to scratch your head and ponder “Where does that curly brace go again?”
//define a simple variable
var x = 5;
if (x === 5) console.log("Success"); //Success
//with curly braces for multiple statements
if (x === 5) {
console.log("Success");
//more statements
}
//deliberately make condition false
if (x === 6) console.log("Success"); //undefined
//there is nothing to do if condition is false so
//lets add an else clause
if (x === 5) {
console.log("Success");
} else {
console.log("Not successful");
}
//use else...if clause to check for another
//condition if previous was false
if (x === 5) {
console.log("Success");
} else if (x === 6){
console.log("Success also");
} else {
console.log("Not successful");
}
//alternatively check for multiple conditions
//with OR operator instead of else..if
if (x === 5 || x === 6 || x === 7) {
console.log("Success");
} else {
console.log("Not successful");
}
//or use a switch statement
switch(x) {
case 5:
console.log("Success");
break;
case 6:
console.log("Success");
break;
case 7:
console.log("Success");
break;
default:
console.log("Not successful");
}