Variables and Datatypes in JavaScript
Variables and Data Types in JavaScript are fundamental concepts used to store and manage data in a program. They define how information is declared, stored, and manipulated during execution.
- Variables: Declared using var, let, and const to store data values.
- Primitive Data Types: Includes Number, String, Boolean, Null, Undefined, BigInt, and Symbol.
- Non-Primitive Data Types: Includes Object, Array, and Function used to store complex data.
Variables
A variable is like a container that holds data that can be reused or updated later in the program. In JavaScript, variables are declared using the keywords var, let, or const.
- var Keyword The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behaviour.
var n = 5;
console.log(n);
var n = 20; // reassigning is allowed
console.log(n);
- let Keyword The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same scope.
let n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
console.log(n)
- const Keyword The const keyword declares variables that cannot be reassigned. It's block-scoped as well.
const n = 100;
// n = 200; This will throw an error
console.log(n)
4.without keyword
In this we can reinsilate and it prints the last value declare.
i=10;
i=20;
console.log(i)
Data Types
JavaScript supports various datatypes, which can be broadly categorized into primitive and non-primitive types.
Primitive Datatypes
Primitive datatypes represent single values and are immutable.
- Number: Represents numeric values (integers and decimals).
let n = 42;
let pi = 3.14;
- String: Represents text enclosed in single or double quotes.
let s = "Hello, World!";
- Boolean: Represents a logical value (true or false).
let bool= true;
- Undefined: A variable that has been declared but not assigned a value.
let notAssigned;
console.log(notAssigned);
- Null: Represents an intentional absence of any value.
let empty = null;
- Symbol: Represents unique and immutable values, often used as object keys.
let sym = Symbol('unique');
- BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.We can denote BigInt by using n at the end or the values.
let bigNumber = 123456789012345678901234567890n;
Non-Primitive Datatypes
Non-primitive types are objects and can store collections of data or more complex entities.
- Object: Represents key-value pairs.
let obj = {
name: "Amit",
age: 25
};
- Array: Represents an ordered list of values.
let a = ["red", "green", "blue"];
- Function: Represents reusable blocks of code.
function fun() {
console.log("GeeksforGeeks");
}

Top comments (0)