Understanding JavaScript
Data Types



What are Data Types in JavaScript?

In JavaScript, data types define the type of value a variable can hold. Understanding data types is crucial because they determine what kind of operations can be performed on the data.

Categories of Data Types

JavaScript has two main categories of data types:

Primitive Data Types Explained

1. String

Represents textual data. Strings can be enclosed in single, double, or backtick (template) quotes.


let name = 'Alice';
let greeting = "Hello";
let template = `Hi, ${name}!`;
console.log(template);
    

Output:

Hi, Alice!

Q: Can I use single and double quotes interchangeably in strings?

A: Yes, as long as you match the opening and closing quotes correctly.

2. Number

Represents both integer and floating-point numbers.


let age = 25;
let price = 99.99;
console.log(age, price);
    

Output:

25 99.99

3. BigInt

Used to represent very large integers beyond the safe integer limit for numbers.


let bigNumber = 1234567890123456789012345678901234567890n;
console.log(bigNumber);
    

Output:

1234567890123456789012345678901234567890n

Q: Why do we use n at the end of the number?

A: It tells JavaScript this is a BigInt type, not a regular number.

4. Boolean

Represents logical values: true or false.


let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn, hasPermission);
    

Output:

true false

5. null

Represents the intentional absence of a value.


let data = null;
console.log(data);
    

Output:

null

6. undefined

Indicates a variable has been declared but has not yet been assigned a value.


let value;
console.log(value);
    

Output:

undefined

7. Symbol

Represents a unique and immutable value, often used for object keys.


let id = Symbol("userID");
console.log(id);
    

Output:

Symbol(userID)

Reference Data Types

1. Object

Used to store collections of data and complex entities.


let person = {
  name: "Alice",
  age: 30
};
console.log(person.name);
    

Output:

Alice

2. Array

Used to store multiple values in a single variable.


let fruits = ["apple", "banana", "cherry"];
console.log(fruits[1]);
    

Output:

banana

3. Function

Functions are objects that can be called (invoked).


function greet() {
  return "Hello!";
}
console.log(greet());
    

Output:

Hello!

How to Check the Data Type

Use the typeof operator to check the type of a variable.


let age = 25;
console.log(typeof age); // "number"
console.log(typeof null); // "object" (quirk in JavaScript)
    

Output:

number
object

Q: Why does typeof null return "object"?

A: It's a long-standing bug in JavaScript that's kept for backward compatibility.

Summary Table

TypeCategoryExample
StringPrimitive"hello"
NumberPrimitive42
BigIntPrimitive123456789n
BooleanPrimitivetrue
nullPrimitivenull
undefinedPrimitiveundefined
SymbolPrimitiveSymbol("id")
ObjectReference{name: "Alice"}
ArrayReference[1, 2, 3]
FunctionReferencefunction() {}

Conclusion

Understanding data types is foundational in JavaScript. It helps you write better, bug-free code and understand how JavaScript manages memory and data internally.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M