Let’s Learn More about JavaScript

Al-Junaed Islam
3 min readMay 8, 2021

I am guessing that we all know less and more about JavaScript. Still, I am giving a little bit of an introduction of JS below. Then we will go through some detailed things about JS that you might or might not know.

Photo by Artur Shamsutdinov on Unsplash

JavaScript

JavaScript (JS) is a programming language. In detail, we can say JS is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions. It is also a scripting language for web pages. As it is an interpreted language, that’s why it does not need any compilation before running the code. An interpreter reads the code and runs it line by line. Today we will learn more about JS’s tricky topics.

Event Bubble

We know that what is DOM and how it works. If you not, then you can give a read from here. Event bubble works in DOM. First of all, it searches/captures events from top to bottom of the DOM. After finding the events, it first executes the children’s event. If there is not any, it will go to its parent element and run the event. For example, our HTML structure is

Example HTML structure to understand

We have an on-click event on <div> and <ul>. When we click on any of the <li> according to the event bubble, first, it will try to execute any event in <li>. As li has not any event, then it will execute <ul>’s event then <div>’s event.

Function with Default Parameter

While calling a function in JS sometimes we send parameters to it. What if we send only one parameter while it takes two values as parameters, then what will happen? You may say we will get an error. Yeah, you are right. But there is an option to not get any error which is to set the default value for the parameter. Let’s understand with an example,

Here we can see the in line 5, the output is 4 as our parameters were 2,2. But in line 6 our output is 2, why? Because we set a default value for parameter2 which is 0. So it returns 2+0=2.

Scope

The scope is like a boundary for variables. Let me show you some examples,

So we can see that we declare a variable in the scope of the “if” condition. So it makes a boundary of the variable’s value inside the “if”. That’s why we can’t get the output outside of “if” while we get output inside of it. If we declare the variable out of “if” then we can print it on line 6. The same thing goes for function too.

Closure

When you call/return a function from another function, it will create a close environment. For example, we have a function A, which returns function B. Also, function A has a variable C. Now we call function A from new variable X and variable Y. So, we will have function B in our two new variables. As it creates a close environment, variable A and Variable B will have variable C, but the changes of variable C will be different for both of them.

Coming Soon…

Bind, Call and Apply

Window

Global Variable

This

Asynchronous

Event Loop

API

Call Back Function

--

--