Variables In JavaScripts



(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

Hello everyone, I hope you are having a good day. In today’s tutorial, I am going to tell you about “Variables in JavaScript” .
The topic, I am going to highlight today, is how to declare variables in JavaScript and types of Variable? JavaScript has variables too as many other programming languages, so let’s get started with it:
What are Variables In JavaScript
Variable means anything that can vary(change). In JavaScript, Variable holds the data value, in simple words, JavaScript variables are containers and can store the data value. Variable in JavaScript are declared with var (reserved keyword).

It is the basic unit for holding or storing the data.
A variable value can change at any time.
Variable must have a unique name.

Syntax
var (Variable-Name) = value;
Variable declaration

var number = 5; // variable store numeric value
var string = “Hello” // variable store string value

(adsbygoogle = window.adsbygoogle || []).push({});

In this example, we have declared two variables using var : number, string. we have assigned each variable a different value. The data value of the number is ‘5’ and string data value is ‘Hello”. Let’s have a look at few other examples:
In this  picture, you can see that

x store the value of 9
y store the value of 2
z store the value of 7

JavaScript is a dynamically typed language . In simple words, Its data types are converted automatically as needed during script execution.
For example in c# language, we have to create an integer variable to use for numerics.

int X = 5;

To create a string variable we use string keywords

String str = “Learning”
But in JavaScript, Variable will be automatically detected and you don’t have to define their data type that if they are integer or string etc.

var myVarname = 10;
alert(myVarname);
myVarname = “This is a string value”;
alert(myVarname)

Look in this above example, in the beginning, myVarname variable is treated as int numeric type and with alert. Following that, we have worked on myVarname again as a string and showed it. May you have noticed that we have not defined their data types.
How to Declare A Variable in JavaScript
Storing the value of a variable is called variable initialization.  You can store the value of the variable at the time of variable creation(in other word declare ) or later when you need to call that variable.

Example:

var name = “Justin”;
var salary;
salary = 10,000;

(adsbygoogle = window.adsbygoogle || []).push({});

You can create a variable or declare the value of the...

Top Tech News


Top