10 Important topics of JavaScript
1. Truthy and Falsy values:
In the boolean expression, Truthy values are values that evaluate True and Falsy values are values that evaluate False. Again, in numeric value, falsy values mean 0 (zero) and Truthy values mean 1 (one).
There is six falsy value in JavaScript:
false
null
0
undefined
NaN
""
const a = NaN;const b = false;const c = null;console.log(a); // falseconsole.log(b); // falseconsole.log(c); //false
Bellow some truthy values:
true
{ }
[ ]
23
'hello'
newDate()
const a = true;const b = “hello world”;const c = 45;console.log(a); // trueconsole.log(b); // trueconsole.log(c); // true
2. Null vs Undefined:
Null means nothing, it is an assigned value. But Undefined means a variable has been declared but not defined.
const a = null;const b;console.log(a); // nullconsole.log(b); // undefined
3. Double equal ( == ) vs Triple equal ( === ):
Double equal (== ) is used for comparing two variables, but it can not check the data type of variable. On the other hand, Triple equal ( === ) is used for comparing two variables and it can check the data type of variable.
const a = ‘hello’;const b = ‘hello’;console.log( a == b ); // trueconsole.log( a === b ); // trueconst c = hello;const d = ‘hello’;console.log( c == d ); // trueconsole.log( c === d ); // false
4. Call():
Call and apply ara very similarly. call() is a method that takes the arguments separated by a comma and invokes a function with a given ‘this’ value.
const friend1 = { firstName: ‘Fahim’ , lastName: ‘Ahammed’ };const friend2 = { firstName: ‘Kuddus’ lastName: ‘Ali’ };function Friend ( gretting1, gretting2 ) {console.log( gretting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘ ‘ + gretting2 );}Friend.call( friend1, ‘Hay’, ‘What’s up?’ ); // Hay, Fahim Ahmmed, What’s up?Friend.call( friend2, ‘Hay’, ‘What’s up?’ ); // Hay, Kuddus Ali, What’s up?
5.apply()
apply() is a method that takes the array of arguments.
const friend1 = { firstName: ‘Fahim’ , lastName: ‘Ahammed’ };const friend2 = { firstName: ‘Kuddus’ lastName: ‘Ali’ };function Friend ( gretting1, gretting2 ) {console.log( gretting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘ ‘ + gretting2 );}Friend.apply( friend1, [ ‘Hay’, ‘What’s up?’ ] ); // Hay, Fahim Ahmmed, What’s up?Friend.apply( friend2, [ ‘Hay’, ‘What’s up?’ ]); // Hay, Kuddus Ali, What’s up?
6. blind()
blind () is a method that creates a new function. But it does not call the function.
const friend1 = { firstName: ‘Fahim’ , lastName: ‘Ahammed’ };const friend2 = { firstName: ‘Kuddus’ lastName: ‘Ali’ };function Friend ( gretting1, gretting2 ) {console.log( gretting1 + ‘ ‘ + this.firstName + ‘ ‘ + this.lastName + ‘ ‘ + gretting2 );}const inviteFriend1 = Friend.blind ( friend1 );const inviteFriend2 = Friend.blind ( friend2 );console.log( inviteFriend1 ( ‘Hello’, ‘Whats up? ‘ ));// Hello, Fahim Ahammed Whats up?console.log( inviteFriend2 ( ‘Hello’, ‘Whats up? ‘ ));// Hello, Kuddus Ali Whats up?
7. Global Variable;
A global variable declared outside a function. It can be accessed any function.
const value = 100;function a () { console.log( value + 50 ) ; // 150}function b () { console.log( value * 5 ) ; // 500}
8. this keyword
The this keyword refers to the current object in a method or constructor.
const Person = { firstName: ‘Akbor’, lastName: ‘Ali’, age: ‘23’, fullName: function () { return this.firstName + “ “ + lastName; }}
9. Private Variable:
A private variable is accessible in the current class or function. It cannot access all functions or classes.
const a = 100;if ( a < 80 ) { const b = 50;} else { const c = 120;}console.log( a ); // 100console.log( b ); // b cannot be accessableconsole.log( c ) ; // c cannot be accessable
10. Global Scope:
A global variable declared at the top of a program or outside a function and a global variable has global scope.
const a = “hello”;function test () { console.log(a);}test(); // hello
Thank you… :)