ARRAY & LOOPS IN JAVASCRIPT WITH EXAMPLE | PART-1

Share the post
In any programming language, array & loops are the basic building blocks. They are the most important things you have to know before starting to learn advanced versions of any language. In this tutorial, We will learn what an is array? & How to get the values from an array. Also, We will see the basic if & if-else. I have divided this tutorial into two parts. In the first part, We will see array & if, if-else. The array & loops in Javascript with example is explained in this topic.

What is an array?

The array is the collection of items. Multiple values are stored inside a single variable.
Example :
 

var array = [“A”, “B”, “C”, “D”, “E”];
If you console this array-like following way.

 var array = [“A”, “B”, “C”, “D”, “E”];
 console.log(array);

You will get following output :
 
output
if you noticed in the above image it’s observed that array assign an index to every particular value. Like in the 0th index you will get the value of “A”. If you want to get the value of a particular index like below.

var array = [“A”, “B”, “C”, “D”, “E”];
console.log(array[2]);

For the above code, You will get the value is “C”. As the array always starts with index 0.

if STATEMENT IN JAVASCRIPT :

If the statement, conditions are true then only the code inside the statement will execute, otherwise not.
Example:
 

var a = 1;
if (a == 1) {
        console.log(a);
}
Output : 
1

if-else STATEMENT IN JAVASCRIPT :

An if-else statement, if the condition inside if is true then it will execute the code inside if statement & if the statement is false then it will execute the code of else statement.
Example : 
 

    var a = 1;
    if (a == 2) {
        console.log(“INSIDE IF”);
    } else {
        console.log(“INSIDE ELSE”);
    }
 
Output : 
INSIDE ELSE
In part-2 we will learn about for loop, do-while loop, while loop.

2 thoughts on “ARRAY & LOOPS IN JAVASCRIPT WITH EXAMPLE | PART-1”

Leave a Comment