Posts

Showing posts from September, 2022

Difference between var, let and const keyword in JavaScript

 In JavaScript, We can declare a variable using below keywords: var let const Below are the difference between these keywords: Scope: The scope of the "var" keyword is the global scope. Means variables defined outside the function can be accessed globally. <script>     var a = 20;     function fun() {         if (true) {              var b = 30 // declared in "if" block but can be accessible outside of this "if" block.         }         console.log(b);         console.log(a);     }     fun(); </script> Output:  30 20 The scope of a "let" variable is only block scoped. It can’t be accessible outside the particular block. Have a look at below example: <script>     let a = 20;     function fun() {           if (true) {          ...

Difference between NULL and Undefined in Javascript

Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript. Undefined:  When we declares a variable but doesn't assign any value to it. It means the value does not exist in the compiler. It is the global object. Below are some difference between NULL and Undefined in JavaScript: 1. NULL is assignment value. Means we can assign NULL as a value to a variable. But Undefined is not an assignment value. Example:  let firstName = NULL; // No error let firstName = undefined; // Throw error 2. Type of NULL is "Object" but type of undefined is "Undefined". Example: console.log(typeof null) // Output: object console.log(typeof undefined) // Output: undefined null == undefined // true null === undefined // false