DEV Community

loading...
Cover image for  Learn JavaScript the Eloquent Way- Arrays , Objects and Functions

Learn JavaScript the Eloquent Way- Arrays , Objects and Functions

Envoy-VC
・1 min read

πŸ³οΈβ€πŸŒˆ 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
Enter fullscreen mode Exit fullscreen mode

πŸ³οΈβ€πŸŒˆ 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']);

Enter fullscreen mode Exit fullscreen mode

πŸ³οΈβ€πŸŒˆ 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');
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

πŸ“£ Note- I will talk about objects and arrays in more details in the upcoming posts.

Discussion (0)