3.Primitives and Objects in JavaScript

ยท

4 min read

In JavaScript, data types can be broadly categorized into two main groups: primitives and objects. Understanding the distinction between them is crucial for writing effective and efficient JavaScript code. Here's an overview of both primitives and objects:

Primitives:

Primitives are basic data types in JavaScript. They are immutable, meaning their values cannot be changed. There are six primitive data types in JavaScript:

  1. String:

    • Represents a sequence of characters.

    • Example: let str = "Hello World";

  2. Number:

    • Represents numeric values, including integers and floating-point numbers.

    • Example: let num = 42;

  3. Boolean:

    • Represents either true or false.

    • Example: let isTrue = true;

  4. Undefined:

    • Represents an uninitialized variable.

    • Example: let undefinedVar;

  5. Null:

    • Represents the absence of a value or a deliberate non-value.

    • Example: let nullVar = null;

  6. Symbol:

    • Introduced in ECMAScript 6, symbols represent unique and immutable values.

    • Example: let sym = Symbol("unique");

Objects:

Objects are complex data types in JavaScript that can hold multiple key-value pairs. They are mutable, meaning their values can be changed. Objects in JavaScript can be created using curly braces {} or the Object constructor. There are various built-in object types, including:

  1. Object:

    • The most general-purpose object type.

    • Example:

        let person = {
          name: "John",
          age: 30,
          isStudent: false
        };
      
  2. Array:

    • An ordered list of values, indexed by integers.

    • Example:

        let fruits = ["apple", "orange", "banana"];
      
  3. Function:

    • A reusable block of code that can be executed by calling it.

    • Example:

        function add(a, b) {
          return a + b;
        }
      
  4. Date:

    • Represents dates and times.

    • Example:

        let currentDate = new Date();
      
  5. RegExp:

    • Represents regular expressions for pattern matching.

    • Example:

        let pattern = /[a-zA-Z]+/;
      
  6. Map, Set, WeakMap, WeakSet:

    • Specialized objects introduced in ECMAScript 6 for specific use cases.

Understanding the difference between primitives and objects is essential for working effectively with JavaScript. Primitives are passed by value, while objects are passed by reference. This difference has implications for how data is manipulated and shared in your code.

Objects are a fundamental part of JavaScript, allowing you to store and organize data in key-value pairs. Here's a beginner-friendly overview of working with objects in JavaScript:

Creating Objects:

  1. Object Literal:

    • The simplest way to create an object is by using the object literal notation (curly braces {}):

        let person = {
          name: "John",
          age: 30,
          isStudent: false
        };
      
  2. Object Constructor:

    • You can also create objects using the Object constructor:

        let person = new Object();
        person.name = "John";
        person.age = 30;
        person.isStudent = false;
      

Accessing Properties:

You can access object properties using dot notation or square bracket notation:

console.log(person.name); // Output: John
console.log(person["age"]); // Output: 30

Modifying Properties:

person.age = 31;
person["isStudent"] = true;

Adding Properties:

person.city = "New York";
person["gender"] = "Male";

Removing Properties:

delete person.isStudent;

Object Methods:

Objects can also contain functions, known as methods:

let person = {
  name: "John",
  age: 30,
  sayHello: function() {
    console.log("Hello, my name is " + this.name);
  }
};

person.sayHello(); // Output: Hello, my name is John

Object Iteration:

You can loop through an object's properties using for...in loop:

for (let key in person) {
  console.log(key + ": " + person[key]);
}

Object Destructuring (ES6+):

let { name, age } = person;
console.log(name); // Output: John
console.log(age); // Output: 30

Object Shorthand (ES6+):

When the property name and variable name are the same, you can use shorthand:

let name = "John";
let age = 30;

let person = { name, age };

Object Methods (ES6+):

Using arrow functions for object methods:

let person = {
  name: "John",
  sayHello() {
    console.log("Hello, my name is " + this.name);
  }
};

person.sayHello(); // Output: Hello, my name is John

Objects in JavaScript are versatile and can be used in various scenarios, such as modeling real-world entities, organizing data, and creating reusable code structures. Understanding how to work with objects is a key aspect of mastering JavaScript.

If you don't understand any part just relax ๐Ÿ‘Œ .

U will know it later

ย