Some important features of JavaScript
1. Truthy and Falsy values:
In JavaScript ,Fasly Values ate or empty string,undefined, null,NaN. false
And Truthy Values are ‘0’,[],{},
const num=-4;
if (num){
console.log(‘condition true’);
}
else {
console.log(‘condition false’);
}
condition true
2.Undefined vs Null:
There are many reason to be undefined is as follws:
Variable declared without values
let jaman
console.log(jaman);
undefined
Function without return
Function without passing value of parameter
Unavailable property of a object
Null: Null means exits no value . Null in JavaScript is an assignment value. You can assign it to a variable.
var demo=null;
console.log(demo);
null
3.Scope,Block-scope:
At First, we talk about function scope. In this case, we can access and get output within a function but outside of the function becomes undefined ,here is example:
function multiply(num1,num2){
let result=num1*num2
console.log(result);
return result;
}
const output=multiply(5,2)
console.log(output);
But the above example , if I try console.log outside of the function it will be shown
Undefined .because its scope within this function is called function scope.
If any variable declare outside of the function can be executed from anywhere .
This is called global scope.
let num3=4;
function multiply(num1,num2){
let result=num1*num2*num3
console.log(result);
return result;
}
const output=multiply(5,2,3)
console.log(num3);
console.log(output);
40
4
40
4.Apply vs. Call vs. Bind:
a.Function.prototype.call()
The method Call invokes the function and allows you to pass in arguments one by one using commas.
let customer1 = { name: ‘Leo’, email: ‘leo@gmail.com’ };
let customer2 = { name: ‘Nat’, email: ‘nat@hotmail.com’ };
function greeting(text) {
console.log(`${text} ${this.name}`);
}
greeting.call(customer1, ‘Hello’); // Hello Leo
greeting.call(customer2, ‘Hello’); // Hello Nat
b.Function.prototype.apply()
The method Apply invokes the function and allows you to pass in arguments as an array.
let customer1 = { name: ‘Leo’, email: ‘leo@gmail.com’ };
let customer2 = { name: ‘Nat’, email: ‘nat@hotmail.com’ };
function greeting(text, text2) {
console.log(`${text} ${this.name}, ${text2}`);
}
greeting.apply(customer1, [‘Hello’, ‘How are you?’]); // output Hello Leo, How are you?
greeting.apply(customer2, [‘Hello’, ‘How are you?’]); // output Hello Natm How are you?
c.Function.prototype.bind()
The Bind method returns a new function, allowing you to pass in a this array and any number of arguments. Use it when you want that function to later be called with a certain context like events.
let customer1 = { name: ‘Leo’, email: ‘leo@gmail.com’ };
let customer2 = { name: ‘Nat’, email: ‘nat@hotmail.com’ };
function greeting(text) {
console.log(`${text} ${this.name}`);
}
let helloLeo = greeting.bind(customer1);
let helloNat = greeting.bind(customer2);
helloLeo(‘Hello’); // Hello Leo
helloNat(‘Hello’); // Hello Nat
The Bind implementation would be like this:
Function.prototype.bind = function(context) {
var fn = this;
return function() {
fn.apply(context, arguments);
};
};
Call and Apply are interchangeable. You can decide whether it’s easier to send in an array or a comma separated list of arguments. Bind is different. It always returns a new function.
5.find Largest Element of ARRAY:
We can find the largest element of array like the following examples :
var numbers=[20,50,49,25,86,99]
var maxNumber=numbers[0];
for (var i = 0; i < numbers.length; i++) {
const element = numbers[i];
if(element >maxNumber){
maxNumber=element;
}
}
console.log(maxNumber);
output : 99
6.Remove duplicate item from an array:
We can also remove duplicate element in array is given below :
let names=[‘rashed’,’jahed’,’minto’,’kamal’,’hayet’,’minto’,’jahed’]
let uniqueName=[]
for (let i = 0; i < names.length; i++) {
const element = names[i];
const index=uniqueName.indexOf(element)
if (index == -1){
uniqueName.push(element)
}
}
console.log(uniqueName);
[ ‘rashed’, ‘jahed’, ‘minto’, ‘kamal’, ‘hayet’ ]
7.Count the number of words in a string:
We can count the word of strings like this way:
var sentence =”i am rashed web developer want to be a good programmer “
var count=0
for (let i = 0; i < sentence.length; i++) {
const element = sentence[i];
if (element == “ “ && sentence[i-1] != “ “ )
count++
}
count++
console.log(count);
output : 12
8.Reverse String : Reversing a string is perhaps one of the most common interview questions that everyone should know.We can reverse the string into this ways:
(a). The quick & dirty way using built-in methods:
let StringToReverse=”i am rashed web developer want to be a good programmer “
let reverseString=StringToReverse.split(“”).reverse().join(“”)
console.log(reverseString);
output: remmargorp doog a eb ot tnaw repoleved bew dehsar ma i
9. Double equal (==) vs triple equal (===):
It is very popular interview question. Double equal is generally defined the value whereas Triple equal is defined along with value & type
If we use double equal(= =) sometimes output becomes awkward likes this:
const number=10;
const number1=’10'
if (number == number1){
console.log(‘condition is true’);
} else{
console.log(‘condition is false’);
}
OutPut: condition is true
If You use triple (===) , output becomes the right way.
const number=10;
const number1=’10'
if (number === number1){
console.log(‘condition is true’);
} else{
console.log(‘condition is false’);
}
OutPut: condition is false
10. How javascript code is executed:
Everything in JavaScript happens inside an “Execution Context”. Whenever a JavaScript program is run an execution context is created.
var number=10; //line1
function add(n) //line2
{ //line3
var result=n+n; //line4
return result; //line5
}
var result1=add(4); //line6
when we run the above code, a global execution context (GEC) is created A. Creation Phase B. Code Execution Phase