JavaScript Primitive and Non-Primitive Data Types

In JavaScript, there are two main categories of data types: primitive and non-primitive.

Understanding the difference between these two categories is crucial to writing effective and efficient JavaScript code.

Primitive Data Types

Primitive data types in JavaScript are simple, basic data types that represent a single value. There are seven primitive data types in JavaScript: Number, String, Boolean, Null, Undefined, Symbol, and BigInt.

Number

The Number data type is used to represent numbers in JavaScript. This includes both integers and floating-point numbers. For example:

let num1 = 42;
let num2 = 3.14;
console.log(typeof num1); // Output: number
console.log(typeof num2); // Output: number

String

The String data type is used to represent a sequence of characters. Strings are enclosed in quotation marks. For example:

let name = "John Doe";
console.log(typeof name); // Output: string

Boolean

The Boolean data type is used to represent a logical value that can be either true or false. For example:

let isValid = true;
console.log(typeof isValid); // Output: boolean

Null

The Null data type is used to represent a non-existent value. It is commonly used to represent an empty object. For example:

let emptyValue = null;
console.log(typeof emptyValue); // Output: object

Undefined

The Undefined data type is used to represent a declared but unassigned value. For example:

let unassignedValue;
console.log(typeof unassignedValue); // Output: undefined

Symbol

The Symbol data type is a new data type introduced in ECMAScript 6. It is used to create a unique identifier for objects. For example:

let sym = Symbol("My Symbol");
console.log(typeof sym); // Output: symbol

BigInt

The BigInt data type is a new data type introduced in ECMAScript 2020. It is used to represent integers that are larger than (2^53) – 1. For example:

let bigIntValue = 1234567890123456789012345678901234567890n;
console.log(typeof bigIntValue); // Output: bigint

Non-Primitive Data Types

Non-primitive data types in JavaScript are more complex data structures that hold multiple values. The most common non-primitive data types in JavaScript are Array and Object.

Array

An Array is a collection of values. Arrays are used to store multiple values in a single variable. For example:

let names = ["John", "Jane", "Jim"];
console.log(typeof names); // Output: object

Object

An Object is a collection of key-value pairs. Objects are used to store collections of data in a more organized and structured way. For example:

let person = {
  name: "John Doe",
  age: 32,
  job: "Developer"
};
console.log(typeof person); // Output: object

External resources for a more in-depth understanding

Here are some external resources for a more in-depth understanding of primitive and non-primitive data types in JavaScript:

Conclusion

In conclusion, understanding the difference between primitive and non-primitive data types in JavaScript is essential for writing effective and efficient code. Primitive data types are simple and basic data types that represent a single value, while non-primitive data types are more complex and can hold multiple values.

W3TWEAKS
Latest posts by W3TWEAKS (see all)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *