Some Random Tricky Javascript Staffs
Tags: javascript
Published: 2021-05-08 09:50:38

1.**Null vs Undefined**
These are some ways you will get undefined..
a)let name;
console.log(name);//undefined

b)if the function don't return
```
function add(num1,num2){
console.log(num1+num2);
}
const res = add(5,6);
console.log(res);//11 undefined

const res2 = add(5);
console.log(res2);//undefined
```
c)Call an object's property that is missing
```
const movie = {name:'Social Network', director:'david Fincher',year:'2011'};
console.log(movie.cast);//undefined
```
d)If you set a variable undefined 
```
let a = undefined;
console.log(a);
```
Null means empty. We can set a variable null. 

2.**Double equal(==) vs Triple equal(===)**
Double equal doesn't check the type of variable whether triple equal checks.
```
const num1 = 5; //integer
const num2 = "5"; // String
if(num1==num2){
console.log("This is true");
}else{
console.log("This is false");
}
//Output: This is true.

if(num1===num2){
console.log("This is true");
}else{
console.log("This is false");
}
//Output: This is false.
```
3.**Closures vs Block Scope**
**Scope:** Access something from inside of a function. For example,
```
function add(num1,num2){
const res = num1+num2;
return res;
}
console.log(res);//error, because res is a variable inside the function, we can't access it from outside.
```
**Global scope:**When a variable is declared globally, it can be accessed from outside of the function.
```
let total = 100;
function sub(num3){
return total+num3;
}
console.log(total);//here we can access the total variable from outside of the function.
```
**Block Scope:**When you declare a variable inside 2nd brackets. They can't be accessed from outside of the area. 
{} =blocked scope.
```
if(number >10){
let response = 'Happy';
console.log(response);
}

console.log(response); // error
```
**Note:** But if you use var to declare a variable. It will not show any error. 
 ```
if(number >10){
var response = 'Happy';
console.log(response);
}
console.log(response); //no error
```
**Important:** It is called **hoisting** in Javascript when a variable is declared using 'var' inside a scope'{}', it grabs the variable outside of the scope and presents it as a global variable.

**Closures:** when a function returns another function. A closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
```
function increase(){
let count = 0;
return function(){
count++;
return count;
}
}
const res1 = increase();
console.log(res1);//1
console.log(res1);//2
console.log(res1);//3

const res2 = increase();
console.log(res2);//1
console.log(res2);//2

console.log(res1);//4
```
4.**remove duplicate from an array**
```
const name = [2,2,3,3,4,5,6];
const uniqueName = [];
for(let i =0;i<name.length;i++){
let element = name[i];
let index = uniqueName.indexOf(element);
if(index==-1){
uniqueName.push(element);
}
}
console.log(uniqueName);//[2,3,4,5,6]
```
5.**Reverse a String**
```
function reverseString(str){
const reverse ='';
for (let i=0;i<str.length;i++){
let char = str[i];
reverse = char+reverse;
}
return reverse;
}
const sentence = 'Hello programming community';
const result = reverseString(sentence);
console.log(result);
```
6.**Asynchronous**
Javascript works synchronously. By saying that, I mean, javascript code finishes working one line after another. Example...
```
function movie(){
console.log('Ironman1');
}
console.log('Ironman2');
console.log('Ironman3');
//Output: 
Ironman1
Ironman2
Ironman3
```

But we can make javascript asynchronous. We can put a function or task on hold, by using the setTimeout() method.

```
function movie(){
console.log('Ironman1');
}
setTimeout(movie);
console.log('Ironman2');
console.log('Ironman3');
//Output: 
Ironman2
Ironman3
Ironman1
```
**Note:**We can also set a specific time for how long the function will be on hold. 
example, ```setTimeout(movie,10000);//here time is in mili second.```
7.**Event loop and Event Buble**
8.**let const and var**
9.**Recursive function**
10.**Call back function**
11.**CRUD Operation**