Showing posts with label js. Show all posts
Showing posts with label js. Show all posts

Thursday, April 23, 2020

Arrays and basic functions associated with Array in JavaScript

About Arrays in JavaScript:

An array is a list of elements which is to store a large amount of element under a single name. The length of the array in JavaScript is not fixed.

=> Let's define an array with numbers stored form 1 to 5.

let number = [1, 2, 3, 4, 5];

=> You can define an array with string elements.

let animal = ["dog", "Cat", "monkey", "tiger"];

=> We can also define an array with mixed elements.

let data  = [1, 2, "hello", 4];

How to access any index element in JavaScript:
For example, we want to access the second element of the data array;
we will use 

data[1]; 

because of indexing in javascript array start from 0.


Now let's perform some operation on arrays by using build-in methods;

1. Length of the array:

' let numbers = [1, 2, 3, 4];
numbers.length; 
// 4





2. Map function to loop over the array and perform an action on each element in this case double all element and store it in the new array:

let numbers = [1, 2, 3, 4];

let doubleNumbers =  numbers.map(function(num) {


    return 2 * num;

});

console.log(doubleNumbers);

// [2. 4. 6. 8]

So, in the above map function, we are performing the anonymous function which is doubling all the element and returning it to doubleNumbers.


3. Adding a new element at the end of the array (push function):

let numbers = [1, 2. 3. 4];/div>


numbers.push(5);

console.log(numbers);

// [1, 2, 3, 4, 5];


4. Remove element from the end of Array (pop function):

let numbers = [1, 2, 3, 4];

numbers.pop();

console.log(numbers);

// [1, 2, 3]
















Wednesday, April 22, 2020

How to create Function in JavaScript

What are the Functions?
The function is a set of statement that perform some particular task.

How to use Functions in JavaScript?
For that first, you have defined a function and then call it to use it.

Function definition:
The function definition is done by using the function keyword that the name of the function and then with parenthesis with the number of parameters separated by commas, then curly braces including the code to be executed.

For Example: Let's define a function which return the sum of two numbers passed to it.

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

So, above is the function which on call will return the sum of a and b.
Let's break down each line of the function.

So the first line is funciton add(a, b)

The a and b are not compulsory so they are optional.
then there are set of parenthesis in which the code is written here it says

return a + b;

which mean add a and b and return it to the line which called the function.

Calling a Function:

The function is called by using the name of the function with parenthesis including parameters in between the parenthesis.

This is how you call a function;

add(10, 29);

You can also define a function without a name and that function is known as an anonymous function.

The syntax is almost the same.

const add = function(a, b) 
{ 
 return a + b;
}

So here the above function is doing the same work and it doesn't have any name and we are assigning it to add.

Anonymous functions will be used a lot when you do JavaScript programming.

Here the below is the screenshot from chrome javascript console which you can open by pressing F12 when you are in chrome.