Undefined JS


What is undefined?

Undefined is a property with global scope. Undefined is a variable in global scope; its initial property is undefined. Undefined means a variable with no value assigned. Sometimes a function can also return the value undefined when the variables in it don't contain any value. Also, a function returns undefined when it doesn't return any value.

Description 

Undefined is a property you can check in conditional statements like it else statements.
You can assign undefined to a variable also.
E.g. var a = undefined;

Example
    var x; if(x === undefined) { //This statement execute } else { //This statement do not execute  }

    Using typeof operator

    var y; if(typeof x === undefined) { //This will get execute } else { //This do not execute }

    let c;

    console.log(c) //will return undefined

Difference between Null and Undefined

  1. Null means the value doesn’t exist or is not assigned explicitly.
  2. Undefined means the variable is declared but value is not assigned.
  3. Null has no specific type.
  4. Undefined is a type of undefined.
  5. Null is an object.
  6. Undefined is a property or type.
  7. Null represents an intentional absence of value.
  8. Whereas undefined represents an uninitialised variable.
  9. Null keyword is used.
  10. Undefined keywords are used.

Example :

<html> <body> <h2>Knowledge2life</h2> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var x; if (typeof x === "undefined") { txt = "x is undefined"; } else { txt = "x is defined"; } document.getElementById("demo").innerHTML = txt; } </script> </body> </html>

OUTPUT:

Knowledge2life