/  Technology   /  Undeclared and undefined variables in Javascript?
Undeclared and undefined variables in Javascript?

Undeclared and undefined variables in Javascript?

 

An undefined variable is one that has been declared but does not have a value. An undeclared variable is one that does not exist in the programmer at all.

What is undeclared variable in JavaScript?

Undeclared is a variable that has not been properly declared using var or let.

How to declare a variable in JavaScript?

Declare a variable with var, let keywords only if you try to declare variable with const keyword JavaScript throws an error like

Declaring a variable with var keyword

<body>
  <script>
    var a;// Here a declared but not defined.
    console.log(typeof(a));// undefined
    a= 9;
    console.log(typeof(a));// number
    a= "9";
    console.log(typeof(a));// string
  </script>
</body>

Console.log:

c1

Observation:

Declare a variable with the var keyword and check its type. It returns the type as undefined, and when we assign a number or string value to a variable, it returns the type as number or string, respectively.

What is difference of declaring with var & let keywords?

If we check both the above examples, we can declare same variable with var keyword inside same script tag but we cannot declare same variable with let keyword inside same script tag. If you try to declare same variable inside same script tag throws an error like below

 <script>
   let a;// Here a declared but not defined.
   console.log(typeof (a));// undefined
   let a = 9;
   console.log(typeof (a));// number
   a = "9";
   console.log(typeof (a));// string
 </script>

Error Message

Cannot re-declare block-scope variable ‘a’ javascript Ln 8 Col 13
Cannot re-declare block-scope variable ‘a’ javascript Ln 10 Col 13

 Declare a variable with let keyword in 2 different script tags

<body>
  <script>
    let a;// Here a declared but not defined.
    console.log(typeof(a)) //undefined
    a = 9;
    console.log(typeof(a)) //number
  </script>

  <script>
    let b;// Here b declared but not defined.
    console.log(typeof(b)) //undefined
    b = "9";
    console.log(typeof(b)) //string

  </script>
</body>

 Console.log ()

c2

 

Note: Using the const keyword to declare a variable results in an error.

Declare a variable with const keyword throws an error

<script>
  const name;
</script>

Error Message: const declarations must be initialized

‘const’ declaration  must be initialized   javascript Ln 9 Col 11

What is undefined variable in JavaScript?

Undefined is a variable declared but that has not been assigned any value.

Undefined a variable with var keyword

<body>
  <script>
    var num1=9;// Here a declared but not defined.
    console.log(typeof(num1));// number
    var num2;
    console.log(typeof(num2));// undefined
  </script>
</body>

We declare and defined a num1 variable with var keyword. But we are trying to get typeof                          undeclared num2 variable with var keyword. Then output will be :

c3

Undefined a variable with let keyword

<script>
  let num1 = 9;// Here a declared but not defined.
  console.log(typeof (num1));// number
  let num2;
  console.log(typeof (num2));// undefined
</script>

Output:

c4

 Undefined a variable with const keyword

<body>
  <script>
    const num1=9;// Here a declared but not defined.
    console.log(typeof(num1));// number
    console.log(typeof(num2));// undefined
  </script>
</body>

We used the const keyword to declare and define the variable num1. However, we are attempting to acquire the typeof undeclared num2 variable using the const keyword. The output will then be:

Output:

c5

 

Leave a comment