Some Important Topics Of JavaScript
Truthy and falsy Values
When values are set to True, it’s truthy otherwise false. There are only six values are falsy and rest are considered as truthy values.
- false
- undefined
- null
- “”
- NaN
- 0
“null vs undefined”
“null” is a value which is assigned to a variable. It represents no value as a result. But, undefined is a variable type and null is an object.
let x = null;
console.log(x);
//null
You can think undefined as simply that you declared a variable but not defined this.
let x ;
console.log(x);
//undefiend
In spite of some similarities like both are falsy and primitive values, null and undefined is not strictly the same.
var sum1 = 5+ null;
console.log(sum1)
//5var sum2 = 5+ undefined;
console.log(sum2)
//NaN
so, null !==undefined but null == undefined
Double equal (==) and Triple equal(===)
Double equal (==) is used to compare two variables, it ignores data types. On the other side, triple equal (===) is used to compare not only values but also data types.
const num = 3183;
const numString = '3183';
console.log(num == numString) //true
console.log(num === numString) //false
Implicit Conversion
It’s a popular feature in javascript. When you needed to convert an unexpected value type to the expected value type, you can use it.
1 + "3" + 1 //131
true + true //2
1 - true //0
Scope in Javascript
Scope helps to determine the visibility of variables. There are two scopes in javascript like global and local. when we declare a variable as a global, we can call it from anywhere in code. But when we declare a variable as a local in a function, we can use it only inside this function.
var name = 'java';
console.log(name); // 'java'
function showName() {
var name = 'python';
console.log(name); // 'python'
}
console.log(name) //'java'
Block Statement
It’s like if and switch condition. It does not create scope like functions.
if(true){
var name = "jhon";
var age = "33";
}
console.log(name) //'jhon'
console.log(age) //33
“new” keyword in JavaScript
“new” keyword creates a new object. So, the type of it is an object. It modifies “this” and returns the value of “this”.
function Student (name){
this.name = name;
}
let person = new Student("luffy")
console.log(person.name);
Thank you!