Javascript - I didn't know that
My work as a CRM (Salesforce) Developer requires me to know Javascript. Though, I know a reasonable amount of Javascript, I still feel like I don't know anything. So, I'm taking a course on Javascript from codeWithMosh. The course comes in two parts. The first one covers only the basics. I thought of going through that nevertheless. Good that I did, because I realized that there are many basic things that I didn't know about. Follow the post and let me know in the comments below if you knew these already or if I enlightened you with something new and interesting today:
-
It's always a better practice to declare variables with "let" and not "var". (I had been using "Var" all my life now)
-
JS has 2 broad kinds of variable : Primitive/Value Type and Reference Type
-
The Primitive / Value type Variable in further categorized into : String, Number, Boolean, Undefined, Null types (I'm not gonna explain each because it's too obvious)
-
typeof keyword returns the type of the variable declared based on the value assigned to the variable. But, typeof Null returns "Object" and not "null". (Found this interesting because I always thought that typeof null would be "null")
-
The 3 types of Reference Type variables are : Object, Array, Functions
-
When declaring an array in JS, the collection can have values of same or different types.
E.g. :
- Parameter vs Argument : Parameter is what we have during a function declaration. Argument is the actual value we pass for the parameter. (I always thought they are the same. Lol! )
E.g. :
function greet(name){
console.log('Hello' + name);
}
greet('Neha');
Here, the keyword "Name" is the parameter. "Neha" is the argument.
-
== is different from ===.
=== is Strict Equality Operator(Compares value and type)
== is Lose Equality Operator(Compares value only) - It converts the type of what we have on the right side to what we have on the left side and returns the comparison between values. -
In JS, apart from the Boolean False and True, we also have Falsy and Truthy.
Falsy includes things like ' ', false, undefined, null, 0, NaN. Anything that's not Falsy in Truthy. JS can execute logical operation even on non-Boolean variables by considering it as Falsy vs. Truthy instead of False vs. True
So, false || true gives True
false || 'Neha' gives 'Neha'
False || 1 gives 1
false || 1 || 2 gives 1 -
Short-circuiting: If we have a string of logical conditions e.g. False || 1 || 2, the execution will stop after the first result is obtained. Above returns 1. It short-circuits at 1 because it has already found the value of the expression.
-
Power of Logical operator with non-boolean :
If we have a use case where I need to return the default value if user doesn't specify anything and if he does, then return the user specified value, then this comes in handy. E.g. : If user specifies a colour, then display that, else the default colour.
let defaultColour = 'red';
let selectedColour = 'Blue';
let colour = selectedColour || defaultColour;
So, this prevents us from writing several lines of if-else code. If user specifies a colour, that would be displayed. Else, undefined || 'red' will give 'red' i.e. the default colour. (Handy! Isn't it?)
- || is logical OR whereas | is Bitwise OR