Variables and Datatypes in JavaScript

There are majorly 2 kinds of languages. First, one is Statically typed language wherever every variable and

expression sort is already identified at compile time. Once a variable is said to be of a

definite information sort, it cannot hold values of different information varieties. Example: C, C++, Java.

// Java(Statically typed) 
int x = 5 // variable x is of type int and it will not store any other type. 
string y = 'abc' // type string and will only accept string values

Other, Dynamically typewritten languages: These languages will receive completely different information varieties over time. For example- Ruby, Python, JavaScript etc.

// Javascript(Dynamically typed) 
var x = 5; // can store an integer 
var name = 'string'; // can also store a string.

JavaScript is dynamically typewritten (also referred to as loosely typed) scripting language. That is, in javascript variables will receive

completely different information varieties over time. Datatypes are essentially typewritten of information which will be used and manipulated in a very program.

The latest ECMAScript(ES6) normal defines seven information varieties: Out of that six data types are Primitive(predefined).

Numbers: 5, 6.5, 7 etc.
String: “Hello JustTechReview” etc.
Boolean: Represent a logical entity and might have 2 values: true or false.
Null: this kind has only 1 worth: null.
Undefined: A variable that has not been appointed a price is vague.
Object: it’s the foremost vital data-type and forms the building blocks for contemporary JavaScript. we’ll find out about these information varieties in details in additional articles.

Variables in JavaScript:

Variables in JavaScript are containers that hold reusable information. it’s the essential unit of storage in a very program.

  • The value holds on in a very variable are often modified throughout program execution.
  • A variable is just a reputation given to a memory location, all the operations done on the variable effects that memory location.
  • In JavaScript, all the variables should be declared before they will be used.


Before ES2015, JavaScript variables were alone declared victimization the var keyword followed by the name of the variable and semi-colon.

Below is that the syntax to form variables in JavaScript.

var var_name;
var x;

The var_name is that the name of the variable that should be outlined by the user and will be distinctive. These form of names are called identifiers.

the foundations for making a symbol in JavaScript are, the name of the symbol shouldn’t be any pre-defined word(known as keywords), the primary character should be a letter, an underscore (_), or a

dollar sign ($). succeeding characters is also a letter or a digit or an underscore or dollar sign.

fiers. the foundations for making a symbol in JavaScript are, the name of the symbol shouldn’t be any pre-defined word(known as keywords),

the primary character should be a letter, an underscore (_), or a dollar sign ($). succeeding characters is also a letter or a digit or an underscore or dollar sign.

Notice within the upper code sample, we tend to didn’t assign any values to the variables. We are solely spoken communication they exist.

If you were to appear at the worth of every variable within the higher than code sample, it would be vague.

We can initialize the variables either at the time of declaration or additionally later after we wish to use them. Below are some samples of

declaring and initializing variables in JavaScript:

// declaring single variable
var name;

// declaring multiple variables
var name, title, num;

// initializng variables
var name = "Kishan";
name = "Durgesh";

Javascript is additionally called untyped language. This means, that after a variable is formed in javascript victimization the keyword var,

we will store any form of worth during this variable supported by javascript. Below is the example for this:

// creating variable to store a number
var num = 5;

// store string in the variable num
num = "justtechreview";

The higher than example executes well with none error in JavaScript, not like different programming languages.
Variables in JavaScript

may also value easy mathematical expressions and assume its value.

After ES2015, we currently have 2 new variable containers: let and const. currently we tend to shall check up on each of them one by one.

The variable sort Let shares numerous similarities with var however not like var it’s scope constraints. to grasp additional regarding them visit let vs volt-ampere.

Let’s create use of let variable:

// let variable
let x; // undefined
let name = 'Kishan';

// can also declare multiple vlaues
let a=1,b=2,c=3;

// assignment
let a = 3;
a = 4; // works same as var.

Const is another variable sort appointed to information whose worth cannot and can not modification through the script.

Variable Scope in Javascript

Scope of a variable is that a part of the program from wherever the variable

could directly be accessible.
In JavaScript, there are 2 kinds of scopes:

Global Scope – Scope outside the outer operate connected to Window.
Local Scope – within the operate being dead.
Let’s check up on the code below. we’ve got a worldwide variable outlined in initial line in the global scope.

Then we’ve got an area variable outlined within the operate fun().

We are as yet ready to see the estimation of the worldwide variable, yet for the neighbourhood, variable console.log

tosses a mistake. This is on the grounds that now the console.log explanations are available in worldwide degree where

they approach worldwide factors yet can’t get to the nearby factors.

Additionally, any factor characterized in a capacity with a similar name as a

worldwide variable overshadows the worldwide variable, shadowing it.

To comprehend variable extensions in subtleties in JavaScript, it would be ideal if you allude to the article on understanding variable degrees in Javascript.

let globalVar = "This is a global variable"; 

function fun() { 
let localVar = "This is a local variable"; 
} 

fun(); 

console.log(globalVar); 
console.log(localVar);

Output:

We are still ready to see the worth of the world variable, except for native variable

console.log throws a blunder. this can be as a result of currently the console.log statements are a gift in world scope wherever they need access

to global variables however cannot access the native variables.

Also, any variable outlined in a very operate with constant name as a worldwide variable takes

precedence over the world variable, shadowing it. To understand variable scopes in details in JavaScript, please confer with the article on understanding variable scopes in Javascript.

Leave a Comment

error: Alert: Content is protected!!