JavaScript important core concept you want to know
I introduced you some important JavaScript core concept that must be know as a JavaScript developer.
#1 Truthy & Falsy Value
Falsy value
A falsy value is something which evaluates to FALSE, for instance when checking a variable. In simple terms we could say, variables that do not have values or do not exist but it’s more than that. Below are a list of all falsy values
- false
- 0(zero) Both positive and negative
''
- null
- undefined
- NaN
let a = false
let b = 0
let c = -0
let d = ''
let e = null
let f = undefined
let g = NaNconsole.log(Boolean (a)) //false
console.log(Boolean (b)) //false
console.log(Boolean (c)) //false
console.log(Boolean (d)) //false
console.log(Boolean (e)) //false
console.log(Boolean (f)) //false
console.log(Boolean (g)) //false
Truthy value
A truthy value is something which evaluates to truth, for instance when checking a variable. Below are a list of all truthy values
true
{}
An object (whether empty or not)[]
An array (whether empty or not)25
Numbers (whether positive or negative)'true'
Non empty strings'false'
Non empty stringsnew Date()
Date object
#2 null Vs undefined
undefined
undefine
is a primitive value in JavaScript. A variable or an object has an undefine
value when no value is assigned before using it. So we can say that undefined means lack of value or unknown value.
var someThing;
console.log( someThing ); // undefined
function Sum(age1, age2) {
var result = age1 + age2;
}
var result = Sum(15, 20);
console.log(result); // undefined
null
You can assign null
to a variable to denote that currently that variable does not have any value but it will have later on. A null means absence of a value and null
is not exist.
let age = null;console.log(age); // null
#3 double equal (==) vs triple equal (===)
Double Equals (==) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other.
const number = 1234;
const string = '1234';console.log(number == string); //true
console.log(number === string); //falseconsole.log(0 == false); //true
console.log(0 === false); //false
#4 Scope, block scope
Scope defines the lifetime and visibility of a variable. JavaScript has module scope, function scope, block scope, lexical scope and global scope.
Global Scope
Variables defined outside any function, block, or module scope have global scope. Variables in global scope can be accessed from everywhere in the application.
var num = 33;var func = function() {
var num1 = 17;
return ( num + num1 );
}var result = func();
console.log(result); // 50
Function scope
Function scope means that parameters and variables defined in a function are visible everywhere within the function, but are not visible outside of the function.
var func = function() {
var num1 = 17;
}console.log(num1); // Uncaught ReferenceError: num1 is not defined
A variable declared with var
can be re-declared multiple times in the same scope. but Variables declared with let
or const
cannot be re-declared in the same scope
var func = function(){
var num = 1
var num = 2;
console.log(num);
}
func();var func1 = function(){
let num = 1
let num = 2;
console.log(num);
}
func1(); //Uncaught SyntaxError: Identifier 'num1' has already been declared
#5 Block scope
Block scope is defined with curly braces. Variables declared with let
and const
can have block scope. They can only be accessed in the block in which they are defined.
var x = 1;
{
var x = 2;
}
console.log(x); //2
let x = 1;
{
let x = 2;
}
console.log(x); //1