This JavaScript tutorial explains how to declare and use variables with syntax and examples.
Description
Variables are used to store statistics in your JavaScript code. Before you can use a variable in JavaScript, you ought to first declare it. Let’s have a seem to be at how to declare a variable.
Syntax
In JavaScript, the syntax for declaring a variable is:
var variable_name [= new_value];
Parameters or Arguments
variable_name The name of the variable to declare. This is the identifier for the variable and it is case-sensitive. You ought to no longer use a reserved word as a variable name. new_value Optional. If provided, it is the price to assign to variable_name. If the value is a string, it need to be enclosed in either single prices (‘) or double quotes (“).
Example
Let’s take a seem to be at some examples of how to declare a string, variety and boolean variable in JavaScript.
Declare a String Variable
You can define a variable and assign a string value to it in a single declaration.
For example:
var h = 'TechOnTheNet';
or
var h = "TechOnTheNet";
The variable named h has been declared and given the string price of ‘TechOnTheNet’. Notice that you can use both single charges (‘) or double rates (“) when dealing with string values.
You do now not have to declare and assign a cost to the variable in one statement. Instead, you should first declare the variable named h, at which point, the h variable would have no value. Then you should assign a value to the h variable with a 2d statement as follows:
var h;
h = 'TechOnTheNet';
In this example, the var keyword broadcasts the variable called h and the = assigns the string cost of “TechOnTheNet” to the h variable.
Declare a Number Variable
You can additionally outline a variable and assign a wide variety price to it in a single declaration.
For example:
var counter = 15;
In JavaScript, you can declare a variable named counter and supply it the numeric price of 15.
As an alternative, you ought to use two statements to declare and assign the value. The first statement will declare the variable and the second announcement will assign its price as follows:
var counter;
counter = 15;
Declare a Boolean Variable
You can additionally outline a variable and assign a Boolean price to it in a single declaration.
For example:
var found = false;
In JavaScript, you can declare a variable named located and supply it the Boolean cost of false.
As an alternative, you should use two statements to declare and assign the value. The first statement will declare the variable and the second assertion will assign its cost as follows:
var found;
found = false;
Declare Multiple Variables
You can additionally outline multiple variables and assign their values in a single declaration.
For example:
var h = 'TechOnTheNet', counter = 15, found = false;
In JavaScript, you can declare three variables named h, counter and determined with the identical var keyword and assign their values in a single statement.
This would be equivalent to the following 6 statements:
var h;
var counter;
var found;
h = 'TechOnTheNet';
counter = 15;
found = false;
Leave a Review