Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
•6 min read•View as Markdown
Understanding Variables and Data Types in JavaScript

As we all are here to learn about the JavaScript. The first and the most important thing we need to discuss is

What variables are and why they are needed

Variables are the container that stores data. It is just like a box in which you need to keep few things like that only it is a container which holds a data.

Example:

let name = "Ravi Kumar Paswan"
let age = 20;

Here, the name is the variable ( Container ) which holds some data having string type data and same goes for the age which is also a variable having a number data type.

The variables are used to memorize the location of the data which is kept in it. Basically the location of the memory.

How to declare variables using var, let, and const

Variables can be declared by three types and we will know about which is the efficient way to declare a variable and what are the side effects of declaring variables by several keywords.

Here we have three ways through we can declare a variable are let, var and const.

Let: Let is the modern way for the declaration of a variable and it is also block level scoped and it also inherits it's children and it can be reassign its value and also update it.

let name = "Ravi Kumar Paswan"
name = "Ram Kumar Paswan" /* Here we can easily reassigning the value of values of variable. */

let name = "Ram Kumar Paswan" /* Here it will give an error, we cannot redeclare a variable of having same name. */

Var: By using var for the declaration of the value is not considered as a good practice because it is a global scoped which means it can be accessed by anywhere in the entire code, and also can be redeclared of having same name of the variable which can change the whole codebase. It is reassign able too.

var name = "Ravi Kumar Paswan"
name = "Ram Kumar Paswan" /* Here it is not a big deal but when we use this in our loop it will create a big issue and mess up the whole logic. */

var name = "Ram Kumar Paswan" /* And this is the another big issue that we can redeclare a variable having same name. */

/* So, it is not a good practice to use var for variable declaration. */

Const: Here we have another way to declare a variable const it is used to fix the value or the data which has been kept in our variable. It is like giving order to not update the value of the variable nor redeclare.

const pi = 3.14;
pi = 31.4; /* Here we cannot change and redeclare it */

Primitive data types (string, number, boolean, null, undefined)

In JavaScript, primitive data types are the basic types of data used to store simple values. they can just store a simple value which cannot be an object.

Here we have few primitive datatypes :-

String : It stores a data which are in text, characters and it is written inside single quotes ( ' ' ), double quotes ( " " ) and also backticks (``).

let name = "Ravi Kumar Paswan"
/* Her the name Ravi Kumar Paswan is the string. */

Number : We can store the numerical values like integers and decimals and it don't require single/double or a backticks to store the numeric value.

let age = 23;
/* Here the 23 is the numeric value which is stored in age variable */

Boolean : Boolean represents only the value can be true either false. It is used for conditional checking or decision making.

let isName = true;
let isPresent = false;

if( isName ){

 console.log("Yes this name is available ");

} /* Here we are checking the condition if it is true or false */

if( isPresent ){

 console.log("Yes it's Present ");

} /* Here we are doing the same thing with this */

Null : It is used to give a null value to a variable instead of keeping it blank. This represents that now we are not having any value but we will have it later for this we are using null.

let name  = null;
/* We initially don't have the value so we keep it null. But the value exists*/

Undefined : It means that the variable has been declared but the value is not assigned to it.

let value;
console.log(value);

*/ The output will be undefined because we have not given any value to the variable after the initialization. */

Basic difference between let, var and const

We can declare a variable by using var, let and const but few basic difference make them different from each other. If we declare from let we can reassign a value it is block scoped and don't allow redeclaration but when it comes to declare a variable with var it is function scoped and also reassign a value to it and redeclare it, Now if we go for const we cannot update the value which is assigned to it and we cannot redeclare it with having the same variable name.

What is scope (very beginner-friendly explanation)

Scope can be defined as the area provided to them for accessing them. It is like if we are in a room we can access the thing within the room only not outside them. We have three scope available here are global scope, function scope and block scope.

Global Scope : When a variable is declared outside any function or a block it can be accessed from any part of the program.

Example : A school announcement which everyone can hear it.

Function Scope : If we are declaring a variable in a function we can access that particular variable in that function only out of the function there is no existence of that variable.

Example : A message for a classroom where no other class need to deal with it.

Block Scope : Now same as function scope if we declare a variable in a block { } we can access that particular variable in that only. We cannot access it from out of that block.

Example : A discussion of few students in a classroom.

Data types

Part 1 of 1