Can I declare JavaScript variables as specific types?


In this tutorial, we will see if we can declare JavaScript variables as a specific type or not. The JavaScript contains the three reserve keywords to declare the variables: ‘let,’ ‘var’, and ‘const.’

When the user declares the variable using any keyword, it has a different scope. The variable declared with the const keyword always remains constant, and we can’t change its value after initializing it. The variables declared with the let keyword have block scope and can’t be accessed outside its scope. When we declare the variables with the var keyword, it can have the global scope or block scope.

Users can follow the syntax below to declare the variables with the different keywords.

Syntax

let var1 = value;
var var2 = value;
const var3 = value;

So, we have learned to declare the variable using three reserve keywords in JavaScript. Also, users can understand from the above syntax that we can assign any value to the variable. So, we don’t need to declare the variables as specific types.

In JavaScript, when we assign the value to the variable, it decides the variable type accordingly. We can assign any value to the variable in JavaScript, such as number, integer, string, object, function, Boolean, etc.

Users can follow the syntax below to change the type of variable.

let var1 = "TutorialsPoint"; // var1 is of string type
var1 = true; // var1 is changed to Boolean type
var1 = 10.22; // var1 is changed to number type
var1 = {'name' : 'TutorialsPoint'} // var1 is of object type
var1 = function() { // function body } // var1 is of function type

Example 1

In the below example we declare the different variables using the var keyword. We use only var keyword to declare as string, number, boolean, function types, etc.

<html> <body> <h4> Declaring single JavaScript variable with specific types </h4> <div id = "Output"> </div> <script> // Declare a variable as string var str = "Tutorialspoint" document.getElementById("Output").innerHTML += str + "<br>" ; // Decalre a varubale as number var num = 23.43 document.getElementById("Output").innerHTML += num + "<br>" ; // Declare a variable as boolean var bool = true document.getElementById("Output").innerHTML += bool + "<br>" ; // Declare a variable as function var bool = function(){} document.getElementById("Output").innerHTML += bool + "<br>" ; // Declare a variable as object var obj = {'name':"Tutorials Point"} document.getElementById("Output").innerHTML += obj + "<br>" ; </script> </body> </html>

Example 2

In the example below, we have declared the single variable named var1. We are assigning different values to the variable and changing its type. Also, we are rendering the type of variable with the variable value in the output.

<html> <body> <h2> Can I declare JavaScript variables as specific types </h2> <h4> Declaring single JavaScript variable and changing its type </h4> <div id = "Output"> </div> <script> let output = document.getElementById("Output"); let var1 = "TutorialsPoint"; output.innerHTML += "type of " + var1 + " is : " + typeof var1 + " <br/> "; var1 = 20.33; output.innerHTML += "type of " + var1 + " is : " + typeof var1 + " <br/> "; var1 = true; output.innerHTML += "type of " + var1 + " is : " + typeof var1 + " <br/> "; var1 = { 'name': 'TutorialsPoint' }; output.innerHTML += "type of " + var1 + " is : " + typeof var1 + " <br/> "; var1 = function () { // function body }; output.innerHTML += "type of " + var1 + " is : " + typeof var1; </script> </body> </html>

Users have learned that it is not possible to declare a variable as a specific type. As you change the value of a variable, it decides the type of variable. So, users can assign the value of a specific type to the variable, and the variable becomes that type.

Updated on: 22-Aug-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements