Javascript Reference data types

Reference data types are data types in JavaScript that keep track of a memory location in which the data is kept. Reference data types are copied by reference as opposed to primitive data types, which are duplicated by value.

There are three primary reference data types in JavaScript: objects, arrays, and functions.

Objects:

Objects are unordered collections of key-value pairs, where each key is a string or a symbol, and each value can be a primitive data type, a reference data type, or a function.

Syntax:

let obj = {
    key1: value1,
    key2: value2,
    //...
};

Example:

let person = {
    name: "John",
    age: 30,
    address: {
        street: "123 Main St",
        city: "Anytown",
        state: "CA"
    },
    hobbies: ["reading", "traveling", "coding"],
    sayHello: function() {
        console.log("Hello, my name is " + this.name);
    }
};

console.log(person.name);           // Output: John
console.log(person.address.city);   // Output: Anytown
console.log(person.hobbies[1]);     // Output: traveling
person.sayHello();                  // Output: Hello, my name is John

Output:

John
Anytown
traveling
Hello, my name is John

Arrays:

Arrays are ordered collections of values, which can be of any data type, including other arrays and objects.

Syntax:

let arr = [value1, value2, ...];

Example:

let numbers = [1, 2, 3, 4, 5];
let colors = ["red", "green", "blue"];
let matrix = [[1, 2], [3, 4]];

console.log(numbers[2]);            // Output: 3
console.log(colors.length);         // Output: 3
console.log(matrix[1][0]);          // Output: 3

Output:

3
3
3

Functions:

 Functions are objects in JavaScript and can be assigned to variables, passed as arguments to other functions, and returned from functions.

Syntax:

function functionName(parameter1, parameter2, ...) {
    // code to be executed
    return value;
}

Example:

function sum(a, b) {
    return a + b;
}

let result = sum(3, 4);
console.log(result);                // Output: 7

Output:

7

In conclusion, reference data types in JavaScript are powerful tools for working with complex data structures and writing reusable code. Understanding the syntax and usage of these data types is essential for becoming proficient in JavaScript programming.