March 18, 2025

ikayaniaamirshahzad@gmail.com

Understanding JavaScript Data Types – DEV Community


JavaScript is a dynamically typed language, meaning you don’t have to specify a variable’s type explicitly. However, understanding data types is crucial for writing efficient and bug-free code.In this blog, we’ll explore JavaScript’s data types, their characteristics, and how they work.

Image description

Primitive Data Types
JavaScript has seven primitive data types, which are immutable and stored by value.

1. String
A string represents textual data enclosed in quotes.

let name = "Ramyasree";
let greeting = 'Hello, world!';
Enter fullscreen mode

Exit fullscreen mode

Strings can be manipulated using various methods like length, toUpperCase(), toLowerCase(), concat(), etc.

2. Number
JavaScript uses a single Number type to represent both integers and floating-point numbers.

let age = 25;
let price = 99.99;
Enter fullscreen mode

Exit fullscreen mode

Special numeric values include Infinity, -Infinity, and NaN (Not-a-Number).

3. Boolean
A Boolean represents a logical entity that can have only two values: true or false.

let isLoggedIn = true;
let hasAccess = false;
Enter fullscreen mode

Exit fullscreen mode

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

let x;
console.log(x); // undefined
Enter fullscreen mode

Exit fullscreen mode

5. Null
null is an intentional absence of any value.

let y = null;
console.log(y); // null
Enter fullscreen mode

Exit fullscreen mode

6. BigInt
BigInt is used for integers larger than Number.MAX_SAFE_INTEGER.

let bigNumber = 123456789012345678901234567890n;
console.log(bigNumber);
Enter fullscreen mode

Exit fullscreen mode

7. Symbol
Symbols are unique and immutable primitive values used as object keys.

let sym1 = Symbol('id');
let sym2 = Symbol('id');
console.log(sym1 === sym2); // false
Enter fullscreen mode

Exit fullscreen mode

Non-Primitive (Reference) Data Types
Non-primitive data types are objects and are stored by reference.
1. Array
Arrays are special types of objects used to store lists of values.

let colors = ["red", "green", "blue"];
console.log(colors[1]); // green
Enter fullscreen mode

Exit fullscreen mode

2. Object
Objects are collections of key-value pairs.

let person = {
  name: "Alice",
  age: 30,
  isStudent: false
};
console.log(person.name); // Alice
Enter fullscreen mode

Exit fullscreen mode

3. Function
Functions are also objects in JavaScript and can be assigned to variables.

function greet() {
  return "Hello!";
}
console.log(greet()); // Hello!
Enter fullscreen mode

Exit fullscreen mode

4.Classes
Templates for creating objects.

class Person {
  constructor(name) {
    this.name = name;
  }
}
Enter fullscreen mode

Exit fullscreen mode

5.Set
A collection of unique values.

let uniqueNumbers = new Set([1, 2, 3, 3]);
Enter fullscreen mode

Exit fullscreen mode

6.Map
A collection of key-value pairs where keys can be any data type.

let map = new Map();
map.set("name", "Alice");
Enter fullscreen mode

Exit fullscreen mode

7.Date
Used to work with date and time

let today = new Date();
Enter fullscreen mode

Exit fullscreen mode

8.RegExp
Used for pattern matching.

let regex = /hello/i;
Enter fullscreen mode

Exit fullscreen mode

Conclusion
Understanding JavaScript data types is essential for writing robust applications. By knowing how primitive and reference types work, you can avoid unexpected bugs and improve performance.

If you found this helpful, share your thoughts in the comments or follow me for more JavaScript insights!

Happy Coding!!



Source link

Leave a Comment