Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Know about variable declaration , primitive data types and scoping in JavaScript

Updated
6 min read
Understanding Variables and Data Types in JavaScript

There are a lot of saying that variable is container and box to store different type of value in memory . It is just label to the value so whenever It needed we can directly used by variable name and the value is placed in place of variable name. let say in glass we can drink water, milk , juice any liquid type data so glass is variable . same way In programming, It is used to hold value.

In this article , we discussed about variable and data type in JavaScript.

What is Variable and why it is needed ?

It is the name given to the value which is stored in the memory and It is used to access and manipulate the value by variable throughout the program. It is holding the information which can access or change during execution of program.

It is used because of following reasons :It provide name to the value that gonna be stored in memory so It can easily accessed and manipulated during the execution of program

Characteristics of Variables :

  1. Named Storage : It is name which is also called identifier that is given to the memory location where data can be stored. let x = 5 ;

  2. Dynamic Values : The value of variable can change during execution of program . x = x+10;

  3. Data types : It can hold different types of data like number, string, boolean etc.

  4. Scope : The variable has scope . It means where the variable are accessible in the program. If it is bloc scope then it is not accessible outside of that block . global scope variable is accessible overall the program . It depends upon use case in which scope you want to define the variable.


// declaring and initializing the variable 
var yourName = "Lollipop" ;
var price ; // declaring
price = 5 ; // initializing

// used in expressions
const info = "Hi I am " + yourName + " and my price is Rs." + price ;
console.log(info) ; // Hi I am Lollipop and my price is Rs.5

// updating the values
price = 10 ;
console.log("Price : ", price) ;  // Price :  10

// const variable cannot change
const gender = male ;
gender = female ; // Error : Assignment to const variable

Naming convention for variable

There are certain rules for declaring the name of variable in JavaScript . They are :

  1. It only starts with letters or $ or _ .

  2. It doesnot start with numbers like let 1num = 10 ;

  3. No reserved words are allowed to used as variable . There are some reserved words of language like let , const , function , for, some, every , this, while, do, if, else, class, extends, and many which is keywords which is used in programming in JavaScript.

  4. There is convention to write the variable name in camelCase and it must be descriptive and meaningful because It is name and name must be descriptive which is very useful to understand about the code what it is used for .

  5. No space is allowed and others special characters in the name of variable .

Way of declaring variables

There are 3 ways of declaring variables in JavaScript . by using var, let and const . But In Today modern JavaScript we used let and const only .

var is traditional way before es6 . It has few issues .

  1. It is function scoped .

  2. It creates variable in global window object .

  3. It get hoisted means we can access it before declare in code .

but let and const are introduced in es6 and it is block scoped and solve the problems which is in var .

let is same as var by varying nature . It value can be changed during the program but const is constant . it value cannot be changed throughout the program and we need to initialize in one line . like const myName = "Dipesh" ;

Primitive Data Type in JavaScript

Primitive means It is single value and stored and copied by value . There are 7 different primitive types . They are

// primitive data types

// 1. number
let a = 10 ; 
let total = 200.78 ;
let balance = 25_00_000 ;
let age = 23 ; 

// 2. string
let userName = "Dipesh Chaudhary" ;
let getIn = "Chaicode Cohort 2026" ;
 
// 3. Boolean
let isCohortStd = true ;
let isRude = false ;

// 4. null
let gf = null ;

// 5.undefined 
let job ;

// 6. bigint
let aura = 999999999999999999n; // we can say Infinity

// 7. Symbol
const nationalId = Symbol("Dipesh") ;

Simple Scoping Understanding

Scope is the place where we can access the value of variable.

There are Block and Global scope in JavaScript.

// scoping discussion

console.log(userName) ; // global scope
// not inside any function means global 

function goToGym(gymName) {
    console.log("Gym name is ", gymName)
}
goToGym("BodyBuilder")
// console.log(gymName); // gymName is not accessible
// ReferenceError: gymName is not defined

// we can give block scope by curly braces
{
    let myCount = 98 ; 
    const myBirthPlace = "Lahan" ;
    const myMotherTongue = "Tharu , Maithili " ;
}
// console.log(myCount) ; 
// if variable name is not found suppose inside function , or any block like if 
// then it look up outside of it which is also called variable lookup 
// it is called scope chain 
    

Comparison Table

Feature

var

let

const

Scope

Functional Scope

Block Scope { }

Block Scope { }

Redeclaring

Allowed

Not Allowed

Not Allowed

Reassigning

Allowed

Allowed

Not Allowed

Hoisting

Hoisted (initialized as undefined)

Hoisted (but uninitialized)

Hoisted (but uninitialized)

Temporal Dead Zone

No

Yes

Yes

Temporal Dead Zone (TDZ) is the area before declaration and initialization of let and const variable where we cannot access the value before assign the value.

Scoping Diagram

Conclusion

The variable is container like a box which holds the reference of memory location where value is stored. I hope It is worth for reading and understanding the variable and scoping of variable.

Deep dive into Javascript

Part 8 of 14

this series includes all about Javascript which I will learned in Chaicode Webdev cohort 2026.

Up next

Mastering Arrays: Everything You Need to Know to Get Started

Array 101: The Absolute Beginner’s Guide to Data Structures