Let’s Win The JavaScript Game

Abdul Hasib Ovi
4 min readMay 5, 2021

Javascript a new hero

Why Javascript instead of others Languages?

Heroes are most often misunderstood throughout the whole movie and in the last, they win everyone’s heart. Can you relate this with Javascript? Before Node.js no one looked at Javascript. But when Node.js come all things have changed. Now we can use javascript outside the browser and with only one language we can do so many things we cannot even imagine.

Let’s See some basic syntax of Javascript

Data Type

Javascript has around 8 types of data

Number

String

Boolean

Function

Object

Symbol

null

undefined

Converting Strings in different Number bases

If you want to convert a string to a number with a specific base you can use parseInt()

parseInt(“10”,base)

If you want to covert this 10 to octal number system you can use

parseInt(“10”,8) // 12

Changing String’s case

For this we have two build in method called toUpperCase() & toLowerCase()

Let’s we have an string, greetings = “hELLo”

Then,

greetings.toUpperCase() // HELLO

greetings.toLowerCase() // hello

Pre and Post Increment/ Decrement

i++ is called post increment and ++i is called pre increment

Let’s say an example,

var i= 10;

var j = i++ // j = 10 , i=11

var i= 10;

var j = ++i // j = 11 , i=11

It means when we use pre-increment the value of the main variable is increased first then it is assigned to another variable and for post-increment, it is assigned first then increased. Decrement is also like this.

For of loop

For of loop is a beauty of Javascript actually. You can iterate a for loop on object values or array elements with this

for(let value of array/object){

//do something with the value

}

For in loop

Foor in loop is used to iterate a for loop on object property .

for(let value in array/object){

//do something with the property

}

It can also be used on an array which will be iterated on array index

Array length

Lets assume an array ,

a=[1,2,3]

what is the length of the array? 3 right?

Ok if we assign a value in index 99,

a[99]=4

Now tell me what is the length? Think 5 sec.

….. If you are thinking it is 100 you are right because when we assign a value to index 99 the index values between index 2–98 are initialized but not assigned so all of them became undefined and. And so array length is on more than the highest index.

Adding an Item to the end/star of an array

If we want to add a item to the end of the array we can use array.push() method

Lets,

a=[1,2,3]

a.push(4) // [1,2,3,4]

If we want to add a item to the start, we can use array.unshift() method

Lets,

a=[1,2,3]

a.unshift(4) // [4,1,2,3]

Removing an Item to the end/star of an array

If we want to remove a item from the end of the array we can use array.push() method

Lets,

a=[1,2,3]

a.pop() // [1,2]

If we want to add a item to the start, we can use array.unshift() method

Lets,

a=[1,2,3]

a.shift() // [2,3]

Adding and Removing from specific index

From the upper discussion, we can only add or remove items from start or end. What if we need to add or remove items from a specific index? Here we have multiple problems but the solution is one. The splice() method

Let's remove an item at index 2 of array a, First, we have to understand the syntax.

array.splice(starting index,deleteCount,items we want to add)

starting index is the target index, delete count is the number of items we want to remove, the third portion will be described in the next section.

Lets,

a=[1,3,5]

a.splice(2,1) // [1,5]

As 3 is the item of index 2. So it is removed as we give delete count 1.

The third argument of this method will be needed if we want to add items to a specific index

Lets,

a=[1,3,5]

a.splice(2,0,2) // [1,2,3,5]

Here we give the value for delete count is 0. So no value is removed and 2 is added at index 2.

Immediately Invoked Function Expression (IIFE)

Generally, we declare a function, and to execute the function we have to call it. But if we need to call a function when it is declared we can use IIFE. IIFE is a function expression that calls itself immediately.

Example:

(function(){

console.log(“I am in IIFE”);

}();

The “I am in IIFE” string will be immediately shown in the console, but for regular expression, we had to call the function to print the string in console.

That’s for today. Stay well, Stay safe.

--

--