What is difference between var let and const in Javascript

 var - Global scope when var variable is declared outside a function. It means that if you write following code 


if(true){

    var x = 30

}

console.log(x)

In console it will print 30 so it means x has a global scope because even though it is declared inside a if condition and inside brackets it can be accessed outside the brackets.


if(true){

    let  y = 30

}

console.log(y)



If I run the above code I will get error because let is a block scoped surrounded by {} If it is declared inside a bracket it can be accessed only inside the bracket and if it is declared outside the bracket it can be accessed anywhere and becomes a global variable

Both let and var declared inside a function are available only inside that function.

if(true){

    const  z = 30

}

console.log(z)



const - const is also a scope variable type just like let but only difference is const variable can not be changed as it is constant.

var type of variable can be initialized first and declared later but it is not possible with let and const

t=10

var t;

console.log(t) // will print 10 and no error 



Binary Search Implementation in Javascript

 Suppose we have an array [1,2,3,4,5,6] and array is sorted in ascending order. Now we are asked to find the position of number 5 in the array using binary search. If number is found then return it's index otherwise return -1;

Now let us see binary search algo.

So the idea behind binary search is to keep on dividing our array from it's mid point and move either left or right of the mid point by incrementing low = mid+1 if our target is greater than arr[mid] otherwise 

decrementing from mid (high = mid-1)  if our target is less than arr[mid].

We will keep doing this until low <= high

Now let us understand thorough a picture.


Suppose we have to find index of number 5 in array [1,2,3,4,5,6] using binary search then we will do the following.


Here is the code in Javascript 

arr is [1,2,3,4,5,6]

n = length of array 

k = our target 5

function binarysearch(arr, n, k) {
        
        let low =arr[0];
        let high = arr[n-1];
        
        while(low <= high){
            
            let mid = Math.floor((low + high) / 2);
            
            if(arr[mid]== k)
            return mid;
            
            if(k >arr[mid]){
                low = mid+1;
            }else{
                
                high = mid-1;
            }
            
        }
        return -1;
    }

We will get 4 as the mid of 5 

Sort an array of 0s, 1s and 2s solution in javascript

 function sort012(arr, N)
    {
        
        let one=0;
        let zero=0;
        let two =0;
        
        for(let i=0; i<N; i++){
            
           if(arr[i]==0){
                zero++;
            }else if(arr[i]==1){
                one++;
            }else{
                two++;
            }
  
        }
        
       for(let k=0; k<N; k++){
            if(zero !=0){
                 arr[k]=0;
               zero--
            }else if(one !=0){
                arr[k]=1;
               one--
            }else if(two !=0){
                arr[k]=2;
               two--
            }            
           
        }

        console.log(arr)
        
    }




sort012([0, 2, 1, 2, 0], 5);

Result: (5) [0, 0, 1, 2, 2]

We will count total 1s , 0s and 2s and then in second iteration we will replace the array with count values by using another look

Contact Me

Name

Email *

Message *