Day-1 Blog Writing

Day-1 Blog Writing

Declaring variables in JavaScript

Variable means anything that can vary. JavaScript includes variables which hold the data value and it can be changed anytime. Variables of JavaScript are loosely-typed which means it does not require a data type to be declared. To declare variable JavaScript uses three keywords. That's why often we get confused which one to use.

Var

It declares a function-scoped or globally-scoped variable, optionally initializing it to a value. Outside a function var is a global variable, it can be used anywhere in the code. Though its a global variable it can be changed any where. It can make bugs in code.

That's why ES6 has solved the problem by introducing "let" and "const"

Let

It declares a block-scoped local variable, optionally initializing it to a value. By using "let" we can declare changeable values. If we have a variable that will change we can use let keyword. It is safer and efficient way to declare changeable values. Let unlike var, does not create a property on the global object.

Const

Const is like constant. Once we declare any variable with const we cant change it in our code. its constant and it cant be change anywhere else.

So if we have to declare a constant variable we can use "const" so that the value of the variable will remain the same. On the other hande, If we wanna declare a changeable value we can use "let". Its the more efficient and professional way to declare variables.

Looping in javascript

The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact.

There are four types of loops in JavaScript.

For loop

The for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known.

for (initialization; condition; increment)  
{  
    code to be executed  
}

While loop

The while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known.

while (condition)  
{  
    code to be executed  
}

Do while loop

The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false.

do{  
    code to be executed  
}while (condition);

Recursion

Recursion is a process of calling itself. A function that calls itself is called a recursive function.

function recursion() {
          code
    recursion();
         code
}
recursion();

Here the function named recursion is calling itself in it. This is how recursion works.

Difference between iterative and recursion:

  1. Iterative uses internal state (extra variables for counting, etc). 2.Recursive does not, it simply passes updated parameters between each call.

Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. To solve this problem halting condition is used.

Arrays in JavaScript

Array is a most commonly used data structure. It's used to store multiple values in a single variable.

const array_name = [item1, item2, ...];

Array have a lots of methods:

  • array.push(): This method appends the element in the array.
let array= ["Apple", "Orange"];

array.push("Pear"); 

console.log(array);// ["Apple", "Orange", "Pear"]

In this example we pushed "pear" in the array. So pear has been added to the array.

  • array.pop(): This method takes an element from the end.
let array= ["Apple", "Orange", "Pear"];

console.log( array.pop() ); // ["Apple", "Orange"]
  • array.shift(): This method extracts the first element of the array.
let array= ["Apple", "Orange", "Pear"];

array.shift(); // remove Apple and alert it

console.log(array); // ["Orange", "Pear"]
  • array.unshift(): This method add the element to the beginning of the array.
let array= ["Orange", "Pear"];

array.unshift('Apple');

console.log( fruits ); // ["Apple", "Orange", "Pear"]

This are some basic methods of array. Array have so advanced methods also. Filter, Find, FindIndex, forEach, indexOf, join, map, lastaIndexOf, reduce, reverse, slice, sort, splice this are the advance methods of array.