A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/variables-datatypes-javascript/ below:

Variables and Datatypes in JavaScript

Variables and Datatypes in JavaScript

Last Updated : 04 Jul, 2025

Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, getting a good grasp of these concepts is important for writing code that works well and is easy to understand.

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.

1. var Keyword

The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behaviour.

JavaScript
var n = 5;
console.log(n);

var n = 20; // reassigning is allowed
console.log(n);
2. let Keyword

The let keyword is introduced in ES6, has block scope and cannot be re-declared in the same scope.

JavaScript
let  n= 10;
n = 20; // Value can be updated
// let n = 15; //can not redeclare
console.log(n)
3. const Keyword

The const keyword declares variables that cannot be reassigned. It's block-scoped as well.

JavaScript
const n = 100;
// n = 200; This will throw an error
console.log(n)

For more details read the article - JavaScript Variables

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.

1. Number: Represents numeric values (integers and decimals).

let n = 42;
let pi = 3.14;

2. String: Represents text enclosed in single or double quotes.

let s = "Hello, World!";

3. Boolean: Represents a logical value (true or false).

let bool= true;

4. Undefined: A variable that has been declared but not assigned a value.

JavaScript
let notAssigned;
console.log(notAssigned);

5. Null: Represents an intentional absence of any value.

let empty = null;

6. Symbol: Represents unique and immutable values, often used as object keys.

let sym = Symbol('unique');

7. BigInt: Represents integers larger than Number.MAX_SAFE_INTEGER.

let bigNumber = 123456789012345678901234567890n;
Non-Primitive Datatypes

Non-primitive types are objects and can store collections of data or more complex entities.

1. Object: Represents key-value pairs.

let obj = {
name: "Amit",
age: 25
};

2. Array: Represents an ordered list of values.

let a = ["red", "green", "blue"];

3. Function: Represents reusable blocks of code.

function fun() {
console.log("GeeksforGeeks");
}
Exploring JavaScript Datatypes and Variables: Understanding Common Expressions JavaScript
console.log(null === undefined)

In JavaScript, both null and undefined represent "empty" values but are distinct types. null is a special object representing the intentional absence of a value, while undefined signifies that a variable has been declared but not assigned a value. Despite their similar purpose, they are not strictly equal (===) to each other.

JavaScript

At first glance, this expression may appear to be checking if 5 is greater than 3 and 3 is greater than 2, but JavaScript evaluates it left-to-right due to its operator precedence.

So, 5 > 3 > 2 evaluates to false.

JavaScript

In JavaScript, arrays are objects. Even if two arrays have the same content, they are still different objects in memory.

JavaScript

When JavaScript compares strings, it compares their Unicode values lexicographically (character by character).

JavaScript

In JavaScript, NaN (Not-a-Number) is a special value that represents an invalid number or the result of an operation that cannot produce a valid number.

To check if a value is NaN, use Number.isNaN().

JavaScript

JavaScript uses type coercion with the loose equality operator (==). When comparing true and 1, JavaScript converts true to 1 and then compares the values.

This behavior might lead to unexpected results in some cases, so it’s often recommended to use the strict equality operator (===) to avoid implicit type coercion.

JavaScript
console.log(undefined > 0)

When JavaScript attempts to compare undefined with 0, it converts undefined to NaN (Not-a-Number). Any comparison involving NaN returns false.

JavaScript

The strict equality operator (===) checks both value and type. Since "5" is a string and 5 is a number, the types are different, and the comparison returns false.

JavaScript
console.log([1, 2] == [1, 2])

Even though both arrays contain the same elements, JavaScript compares arrays by reference, not by value.

To check if two arrays are equal, you must compare their contents element by element.

JavaScript
console.log(Infinity > 1000)

In JavaScript, Infinity represents an unbounded, positive number. It's greater than any finite number, including 1000.

For more details read the article - JavaScript DataTypes


Variables and Datatypes in JavaScript


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4