r/Web_Development • u/[deleted] • Jun 25 '18
Advanced Javascript for Beginners: Part 1
It does not take too long to learn the basics of Javascript. However, there is a lot of advanced stuff that you won’t learn in tutorials meant for beginners. In these tutorials, you will learn advanced Javascript.
Const and let.
If you are a beginner, your probably declare a variable like this:
var health = 100;The new way of declaring variables is using “let” or “const”.
let health = 100;
const inflation = 0.03;When you use “const”, you create a constant variable. You can not change it after you declare it. The value will stay the same, no matter what.Following will result in an error:
const price = 100;
price = 50;What is the “let” for then? It can be used to declare variables that can be changed, just like “var”. However, there is a difference between “let” and “var”.
var country = "Germany";
if(true){
var country = "France";
}
//Prints "France"
console.log(country);
let country = "Germany";
if(true){
let country = "France";
}
//Prints "Germany"
console.log(country);If you use “let” inside curly brackets, it creates a new variable. This variable can be accessed only inside curly brackets.
let country = "Germany";
if(true){
let country = "France";
//Prints "France"
console.log(country);
}Ternary operator.
There are more ways of doing control flow than typical if/else statement. If/else is great in most cases, but sometimes it can be easier to use the ternary operator or the switch statement.The ternary operator is just like if/else statement. Except you can do it on one line. The syntax is fairly simple:
condition ? expression : expression2Here is an example:
//Prints Apples
1 === 2 ? console.log("Oranges"):console.log("Apples");
//Prints Oranges
2 === 2 ? console.log("Oranges"):console.log("Apples");I have seen people even use nested ternary operators:
1 === 2 ? console.log("1=2") :
1!==1 ? console.log("1!=1") : console.log("Nested ternary operator");I use ternary operator whenever it feels easier to use it than if/else statement.
Switch Statement.
Basic switch statement:
let fruit = "apple";
switch(fruit){
case "apple":
console.log("There are 5 apples left");
break;
case "orange":
console.log("There are no oranges left");
break;
case "banana":
console.log("There is one banana left");
break;
default:
console.log("We do not sell these fruits");
}Here is how it works:
We have to define the variable that we check. In this case “fruit”.
We check for every possible condition that we want to check using the following syntax: case value
We define default action if any of the conditions did not work.
Keyword “break” stops the switch statement.
Destructuring.
let player = {
health:100,
stamina:20,
speed:5
}If I wanted to extract those values, I could do this:
let health = player.health;
let stamina = player.stamina;
let speed = player.speed;But here is a better way of doing it:
let {health,stamina,speed} = player;Both ways work the same. The first way is just too slow when you have many variables.Next tutorial will come out soon. In it you will learn more about advanced Javascript.Leave a comment if you have any questions. Share this tutorial if you found it helpful.
1
Jun 25 '18 edited Jun 25 '18
When you use “const”, you create a constant variable. You can not change it after you declare it. The value will stay the same, no matter what.
It should be noted that you create a constant reference, you cannot mutate that reference but if your reference is to an object or an array you can change contents/properties of them without error
E.g.,
const myObj = {}; // {}
myObj.someProperty = 'foo'; // {someProperty: 'foo'}
delete myObj.someProperty; // {}
const myArr = []; // []
myArr.push('foo'); // ['foo']
myArr.pop(); // []Will not throw errors, but if you try to re-assign those variables they will throw errors.
const myObj = {}; // {}
myObj.someProperty = 'foo'; // {someProperty: 'foo'}
myObj = {}; // TypeError: Assignment to constant variableAs you advance in your experience you should find yourself avoiding let and using it as a code smell that you can probably improve your code. Variable reassignment can reduce the readability and maintainability of your code. When you're reading your code and you see a const you can be certain that it is not going to mysteriously change it's value, where as a let is a smell that something unexpected might happen to that variable through the scope of it's life.
1
1
Jun 26 '18
With all due respect I feel like most of this is included in beginner JavaScript lessons. At least some I’ve done.
1
u/leadfoot19 Jun 25 '18
Thanks!