← Back to list

Data Types & Variables

JavaScript datatypes and variables

Jaynesh Sarkar · 2025-01-27 09:37 · 1 claps · 2.4 min read
#datatypes-in-javascript #variables-in-javascript #what-is-variable
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Data Types & Variables

JavaScript datatypes and variables

  • What is Variable?
  • Understanding data types
  • Declaring and Initialization variables

What is Variable?

variablessss !!

variablessss !!

A container which stores some data(value) is known as Variable.

  • Where we can store some value.

Understanding data types

A data type determines the kind of value a variable can hold.

JavaScript, being a dynamically-typed language, allows variables to store values of any type, which can even change during runtime.

Types of Datatypes

  1. Primitives Data Type
  2. Non-Primitive (Reference) Data Types

* Primitives Data Types

These represent single values and are immutable.

a. Number

  • Represents numeric values (both integers and floating-point numbers).
let age = 25;       // Integer
let price = 19.99;  // Floating-point
let infinity = Infinity; // Special numeric value

b. String

  • Represents text values, enclosed in single ('), double ("), or template literals (```).
let name = "John Doe";        // Double quotes
let greeting = 'Hello!';      // Single quotes
let template = `Hi ${name}`;  // Template literal

c. Boolean

  • Represents true or false.
let isJavaScriptFun = true;

d. Undefined

  • Represents a variable that has been declared but not assigned a value.
let x; // Undefined
console.log(x); // Outputs: undefined

e. Null

  • Represents an intentional absence of any value (empty value).
let y = null;

* Non-Primitive (Reference) Data Types

These are objects and mutable.

a. Object

  • Represents a collection of key-value pairs.
let person = {
  name: "Alice",
  age: 30
};

b. Array

  • A special type of object used to store ordered collections of values.
let fruits = ["apple", "banana", "cherry"];

c. Function

  • A callable object that performs a specific task.
function greet() {
  console.log("Hello!");
}

d. Date

  • Represents date and time.
let today = new Date();

Different between Var and Let

var and let both are used to declare variables in JavaScript, but they have some important differences.

  • Scope Difference

**var → Function Scoped**

if (true) {
   var name = "Jay";
}
console.log(name); // ✅ works

var ignores block {} scope.

**let → Block Scoped**

if (true) {
   let name = "Jay";
}

console.log(name); // ❌ Error

let only works inside the block where it is created.

  • Re-declaration

var

var age = 20;
var age = 30; // ✅ allowed

let

let age = 20;
let age = 30; // ❌ Error

let cannot be re-declared in same scope.

  • Update Value Both can update values.
var city = "Surat";
city = "Rajkot"; // ✅

let country = "India";
country = "USA"; // ✅
  • Hoisting Both are hoisted, but behavior is different.

var

console.log(a); // undefined

var a = 10;

let

console.log(a); // ❌ Error

let a = 10;

메타데이터
post_id
e8d8ecbffa5e
slug
data-types-variables-e8d8ecbffa5e
url
https://medium.com/@SJaynesh/data-types-variables-e8d8ecbffa5e
canonical_url
https://medium.com/@SJaynesh/data-types-variables-e8d8ecbffa5e
author_url
https://medium.com/@SJaynesh
status
ok
fetched_at
2026-06-29 22:44:20