Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
4 min readView as Markdown
Understanding Variables and Data Types in JavaScript
D
I am a software developer. I have no commitments other than `git commit`.

Welcome back to a new article where we will talk about variables and data types in JavaScript.

What are variables, and why are they needed?

You all have seen some boxes at your home or workplace that are storing something inside them. Variables are just like those boxes, and they store the data. Variables are very essential, and they are needed not only in JavaScript but in any programming language to store the data. Programming is all about playing with data in a logical way. And to play with data, we need to store them. That's why variables are needed.

How to declare variables in JavaScript.

JavaScript provides us three ways to declare variables. Those three ways are:

  1. Var

  2. Let

  3. Const

Using above keyword, we can define variables in javascript like below

var variable1 = 'VAR'
let variable2 = 'LET'
const variable3 = 'CONST'

Basic difference between var, let, and const.

  1. A variable declared with var has global scope and function scope. This means a variable declared inside a function is only accessible inside that function, and if declared outside a function, it is available globally. Whereas a variable is declared with let and const has block scope. This means the variables declared are limited to a block {} where they are declared.

  2. var and let allows you to change the value of variables, but const don't. This means a value assigned to variables declared with const const cannot be changed or reinitialized. But this does not mean that you cannot change the value of properties of keys of an object or the content of an array declared with const. It's just that you cannot reinitialize them.

  3. The variable declared with var can be hoisted. This means the variable can be used even before its declaration because, due to hoisting, its declaration is lifted to the top of the code. Whereas the variables defined with let and const are not hoisted. They cannot be used before their declarations.

Properties Var Let Const
Scope global scope/function scope block scope ({}) block scope ({})
Mutability Value can be change after assignment. Value can be change after assignment. Values cannot be changed after assignment
Hoisting Yes No No
Initialization Optional Optional Mandatory and should initialized at time of declaration

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

Primitive data types are those data types that store single values. Below are some primitive data types present in JavaScript.

  1. string: A datatype that represents textual data enclosed within quotes.
var string1 = "Hey! This is string data types. 2 Good.
  1. number: A datatype that represents numerical data, which can be integer or floating data.
var numberInt = 8000
var numberFloat = 30.55
  1. boolean: A datatype that has only two possible values, and those are true or false.
var booleanTrue = true
var booleanFalse = false
  1. null: It's a special datatype that represents a non-value or absence of value. It's a value that the developer assigns intentionally to represent that there is nothing in the variable.
var nullvalue = null
  1. undefined: It's the datatype and a value that represents that the variable is declared but not initialized.
var undefVar; // Variables is declared but not initialized, value is undefined
var undefVar2 = undefined // Intentionally assigned 

What is scope?

When we hear the word "scope," we think of something about a sniper scope, like 1x, 2x, 8x, etc. But in JavaScript or any other language, the scope means an area where the variable is accessible. The scope is defined by curly braces {}. There are three scopes in JavaScript:

  1. Global Scope

  2. Function Scope

  3. Block Scope

var globalVariable = "Hey! This varaible is present in global scope";

if (globalVariable.length !== 0) {
    let blockScopeVariable = "This is block scope and variable defined with           let and const inside block scope are only accessible inside this scope".
    var varBlockScopeVar = "This is block scope and variable defined with           var inside this scope are treated as global varialbe"
}

function functionScope(){
    var funcVar = "This is function" // This is function scope and variable defined with let, var, and const inside function scope and only accessible inside function
}

This is the end of this article, and I hope you all liked it.

Thank you.

More from this blog