Learn JavaScript the Eloquent Way- Arrays , Objects and Functions

Subscribe to my newsletter and never miss my upcoming articles

🏳️‍🌈 Arrays

As compared to other type-enforced languages,JavaScript arrays are just containers for any type of value, from string to number to object to even another array (which is how you get multidimen‐ sional arrays):

let selectedColours = ['red', 'green'];
console.log(selectedColours);
// ['red','green']

selectedColours[2] = 1
console.log(selectedColours[2]);
// 1

console.log(selectedColours.length)
// 3

🏳️‍🌈 Objects

An object is a group of values; unlike arrays, we can do something better than them:

let person = {
    name: 'John',
    age: 30
};

console.log(person);

// Changing Properties of an object is


//Method 1 Dot Notation

person.name = 'John'
console.log(person.name);

// Method 2 Bracket Notation

person['name'] = 'Mary';
console.log(person['name']);

🏳️‍🌈 Functions

Functions in JavaScript provide organized, reusable code to perform a set of actions. Functions simplify the coding process, prevent redundant logic, and make code easier to follow.

📣 Note- Function Scoping

When you define a function, it creates a scope. Everything defined within the function is not accessible by code outside the function. Only code within this scope can see the entities defined inside the scope.

function greet(name) {
    console.log('Hello ' + name);
}

greet('John');

Nested functions are possible in JavaScript and the same rules apply.

function foo() {
    var a = 'hello';

    function bar() {
        var b = 'world';
        console.log(a); // => 'hello'
        console.log(b); // => 'world'
 }

    console.log(a); // => 'hello'
    console.log(b); // reference error
}

📣 Note- I will talk about objects and arrays in more details in the upcoming posts.

No Comments Yet