Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Datatypes in JavaScript :)

Published
4 min readView as Markdown

Let’s start our journey by adding interactivity to our website using JavaScript, which is nothing but the Big Boss or "Bada Bhai" of web browsers. Let’s dive into its basics by learning about variables, their scope, and data types.

To chaliye suru krte hai…….. 🚀


Variables

Variables are nothing but containers in all programming languages.

Samajh lo ye bas ek container hai jisme hum koi bhi cheez store kar sakte hain, just like storing sugar in a plastic box. The box is your container, and that’s your variable in JavaScript! 🚀

There are three types of variables and each one of it is having different scopes…

Var

The var keyword is used to declare variables in JavaScript. It has functional scope. When you declare a variable, it is either functionally scoped or globally scoped, depending on where you have declared it.

var keyword ki ek unique feature hai ki hum isko easily re-declare kar sakte hain, but this is not a good practice as it can lead to various bugs.

🚫 Avoid var in modern JavaScript to prevent unexpected behaviour and hard-to-debug issues.

var name="Karan"
console.log(name);// output: Karan

var name="Karanraj"
console.log(name); // output: Karanraj

Hoisting

var is hoisted to the top of its scope but initialized as undefined.

  console.log(p); // Output: undefined (hoisting)
  var p = 50;

Let

The let keyword is used to declare variables that have block scope, and it can be re-initialized easily because it is mutable, just like var. However, variables declared with let cannot be re-declared.

Matlab, let keyword se declare kiya gaya variable sirf us specific block tak hi simit rehta hai. Aur uski value ko hum easily change kar sakte hain.

let car="Subaru"
console.log(car) //Output: Subaru
car="Mazda"
console.log(car) //Output: Mazda

Just like Teacher ne bola kal chhutti hai*, phir thodi der baad **Principal sir aate hain aur bolte hain kal chhutti nahi hai!** 😄*

Const

The const keyword is used to declare a variable with a constant value.

Matlab, const keyword tab use karna hai jab kisi value ko constant rakhna ho, jise bad me change nahi kiya ja sakta. Aur iska initialization karna zaroori hota hai*, warna ek **bada error** aa jayega! 😄*

const pi=3.14
console.log(pi) //Output: 3.14
pi= 4.5 // TypeError: Assignment to constant variable.
Featurevarletconst
scopeBlock ScopeFunctional ScopeFunctional Scope
mutabilityYesYesNo
InitialisationNot RequiredNot RequiredRequired
RedeclarationCan be doneCannot be doneCannot be done

Datatypes

There are many datatypes in Javascript. So let’s see the commonly used datatypes.

  • String

    String is simply a series of characters, like your name, which is a string. There are two ways to write a string: using single quotes ' ' or double quotes " ".

let str="Karan"
console.log(str) // Output: Karan
  • Number

    Represent multiple numeric values such as integer, float, double ,etc.

let num=1234
console.log(num) // Output: 1234
  • Boolean

    It represents only tow values that is true and false. Basically you can imagine a bulb whether it is on or bulb is off.

let isOn=true
console.log(`Bulb is on: ${isOn}`) //Output: Bulb is on: true
  • Array

    Array in JavaScript is used to store elements with different data types. One of its biggest advantages is that we can store multiple data types in a single variable.

let arr=["Apple",1234,12.4,'W'];
console.log(arr)// Output: [ 'Apple', 1234, 12.4, 'W' ]
  • Objects

    Objects are used to store properties and methods of anything. Almost everything in JavaScript is an object.

let obj={
    name:"Karan",
    age:18,
}
console.log(obj) //Output: { name: 'Karan', age: 18 }

Conclusion

JavaScript me variables aur data types ka sahi use karna ek achhi coding practice hai.

  • var, let, aur const ka scope aur behavior alag-alag hota hai.

  • Hoisting, mutability, aur redeclaration ke basis par inka use carefully karna chahiye.

  • JavaScript me string, number, boolean, array, aur objects jaise multiple data types hote hain jo alag-alag scenarios me kaam aate hain.

👉 Agar value change karni hai to let use karo, agar fix rakhni hai to const, aur var ka use avoid karo! 🚀