JavaScript Concepts

Photo by Andrew Neel on Unsplash

JavaScript Concepts

·

2 min read

Imperative traverse

a loop where it starts and ends. how the loop is increment and what's the operation, we declare all of these. this is the imperative traversing. e.g

const numbers = [2, 5, 6, 7, 89, 100];
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
    sum += numbers[i];
}
console.log(sum);

Declarative Traverse

we do not need to declare how the loop works. we just need to declare what to do. foreach() method is a declarative traverse of javascript. e.g

const numbers = [2, 5, 6, 7, 89, 100];

numbers.forEach(function () {
    console.log('Hello World');
});

foreach pass a call back function through argument. In this call-back function, there have some parameters. e.g below there are three properties..(values, Index, array). if we need only the index we can write (, index). if we need only the array we can write(, __, array). but this doesn't apply to (arrow function). this will only apply to (normal function).

const numbers = [2, 5, 6, 7, 89, 100];

numbers.forEach(function () {
    console.log(arguments);
});

/* * Output
[Arguments] { '0': 2, '1': 0, '2': [ 2, 5, 6, 7, 89, 100 ] }
[Arguments] { '0': 5, '1': 1, '2': [ 2, 5, 6, 7, 89, 100 ] }
[Arguments] { '0': 6, '1': 2, '2': [ 2, 5, 6, 7, 89, 100 ] }
[Arguments] { '0': 7, '1': 3, '2': [ 2, 5, 6, 7, 89, 100 ] }
[Arguments] { '0': 89, '1': 4, '2': [ 2, 5, 6, 7, 89, 100 ] }
[Arguments] { '0': 100, '1': 5, '2': [ 2, 5, 6, 7, 89, 100 ] }
*/

here, we didn't create foreach function, we just use it. So this is a declarative method.

difference between functions and Methods

There is one major difference between function and method is the function call independently. If a function creates in the object, so this function will be a method. the function always creates and calls independently.

Create Object

there are two ways to create an object. 1. object literal 2. constructor function.

Iterable

That which can be traversed is iterable.