Some important and confusing thing in Javascript

Habibur Rahman
3 min readMay 6, 2021

--

What is javascript?

JavaScript is a client-side and server-side scripting language compacted into HTML pages and is understood by web browsers. Javascript is a single-threaded language that means it can do one task at a JavaScript is also an Object-based Programming language.

What are JavaScript Data Types?

Data types of Javascript are given :

  • Number
  • String
  • Boolean
  • Object
  • Undefined

What is the role of break and continue statements?

The break statement is used to come out of the current loop. In contrast, the continue statement continues the current loop with a new recurrence

Event Loop

The event loop is one of the important and confusing things in javascript. Basically event loop happens when an asynchronous callback function comes.

Let’s know more through example

console.log(‘Hi’);

setTimeout(function cb(){

console.log(‘Hello’);

},5000);

console.log(“Habib”);

Output

Hi

Habib

Hello

We Know JavaScript is a single-threaded language that means one call at a time. Here log(‘Hi’) comes in stack and print Hi then cb comes in the stack but here is an asynchronous callback that’s why after 5sce cb goes to the queue. Then log(‘Habib”) take place in the stack and print Habib. When the stack is empty cb takes place in the stack and prints Hello.

Here setTimeout 5000 means it will run in the future a minimum of 5s.

try-catch

try-catch is used for handling errors in code. try-catch contains two main blocks try and catch. try is used for executing code and catch is used for error handling.

If there were no errors then the catch is ignored.

If any error occurs then the code will stop and the catch will execute.

Let’s clear with two examples:

ex1

try {

alert(‘Start of try runs’);

// …no errors here

alert(‘End of try runs’);

} catch (err) {

alert(‘Catch is ignored because there are no errors’);

}

ex2

try {

alert(‘Start of try runs’);

lalala; //undefine

alert(‘End of try (never reached)’);

} catch (err) {

alert(`Error has occurred!`);

}

Syntax

Basic rule:

Line Length

For a programmer to write a long horizontal line of code is not a good practice. It’s best practice to split them.

Let’s see with an example:

let str = “ ECMA International’s TC39 is a group of JavaScript developers, implementers, academics, and more, collaborating with the community to maintain and evolve the definition of JavaScript”

Cross Browser Testing

First of all, we need to know what is cross-browser testing. Cross Browser Testing is a type of testing to verify if an application works across different browsers as expected. It is the process of verifying your application’s compatibility with different browsers.

Every browser expounds information on the web page differently.

example:

To avoid this kind of problem we need to check our websites on different browsers.

Hoisting

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

If you declare a variable using var it will be moved to the top of their scope before code execution and default value = undefine;

var LANGUAGE= “JAVA”;

var language =”JAVASCRIPT”;

function getL(){

if(!language){

var language= LANGUAGE

}

return language;

}

console.log(getL()); //JAVA

If you declare a variable using let it will be moved to the top of the block and this time default value will not set.

var LANGUAGE= “JAVA”;

var language =” JAVASCRIPT”;

function getL(){

if(!language){

let language= LANGUAGE

}

return language;

}

console.log(getL()); //JAVASCRIPT

Functions with Default Parameter Values

Let’s see with an example:

There is a function named multiply which takes two-parameter and returns multiply of those values. If we pass only one value this function will set the default value b=1.

function multiply(a, b = 1) {

return a * b;

}

console.log(multiply(5, 2)); //output: 10

Here we passed two parameters 5,2 and the function return the multiply of those values.

console.log(multiply(5)); //5

--

--

Habibur Rahman
Habibur Rahman

No responses yet