variable in javascript, javascript variable scope, javascript variable types, use javascript variable in html, examples of variables in javascript
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
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:
variable in javascript, javascript variable scope, javascript variable types, use javascript variable in html, examples of variables in javascript
In this  picture, you can see that
  1. x store the value of 9
  2. y store the value of 2
  3. 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;
You can create a variable or declare the value of the variable later as you can see in the example. Although you can also do variable initialization too. You can declare multiple variables with the single var keyword without storing the value.
var age, height, class, name;
How you can declare a variable in JavaScript with examples
// initializing variable var x = 5; var name = "Adam";
// declaring single variable var name; // declaring multiple variables var age, height, class, name;

Types of Variables

These are two types of JavaScript variables.
  • Local Variables - Local variable will be visible in the only function or block where you have defined it. Function parameters are local to that function and can't define anywhere in the JavaScript.
    variable in javascript, javascript variable scope, javascript variable types, use javascript variable in html, examples of variables in javascript
  • Global Variables - Global variables are variables that will be only visible in a function where it is defined and can be defined anywhere in the javascript.
    variable in javascript, javascript variable scope, javascript variable types, use javascript variable in html, examples of variables in javascript

Things That You Should Keep In Mind

  • Variables can only store a single value of data that can change later.
  • All the variables can only be defined using a var keyword. Variables declared without var keywords becomes global variables.
  • Multiple variables can be declared in a single line like var age = 10; name = "ali"; class = 3;
   JavaScript Reserved Word
These are all the reserved words of JavaScript. All of them can not use as JavaScript Variable, function, loop labels, method or any other objects name.

  JavaScript Reserved Word

abstract

throws catch var implements
Else native final protected delete
instanceof false new function volatile
switch case transient continue return
boolean throw char typeof if
enum long finally private default
int extends null for void
synchronized byte true const public
break this class try goto
export interface float package debugger
goto public void default if
return volatile delete implements short
while do import static with
double in super
  I have explained every point in detail. We hope you understand it clearly. But if you have any question related this topic you can comment below. We will reply to your message. Thank you so much for reading this article and we are looking forward to share the best content with you. Keep reading JavaScript tutorial. We have covered every topic in this  series.