10 Less Talked Things of JavaScript

Abdul Hasib Ovi
4 min readMay 6, 2021

Let’s find the hidden talks

Hello good people. If you are here to sharpen your Javascript concept here then you are in the correct place. We will talk about 10+ things that will boost your knowledge of Javascript I hope.

Data types

There are two types of Data in javascript.

i. Primitive

ii. Non-Primitive

Primitive

Primitive Data types are the types that are defined by Javascript developers. We can only use those types but cannot customize.

i. Number

ii. String

iii. Boolean

iv. null

v. undefined

Non-Primitive

Object is the non-primitive type of data. Users can store different types of data in objects. Array and Function are also other kinds of object

So basically,

  1. Object
  2. Array
  3. Function

These types are the parts of Object Data type.

Number-String Easy Conversion

We often need to convert between number & string. We will see a easy method of doing this,

Let’s say,

var string = “123”

var convertedNumber = + string // 123

Then this “123” will be converted to number from string. We just write a plus sign before the string value.

Let’s say,

var number= 123

var convertedString = “ ” + number// “123”

And now this 123 number will be converted to a string. We just add an empty string with the number. But be careful you have to put the string first position.

Is Javascript Slower?

Ok, we all know JavaScript is a single thread language. Single thread means it can execute only one task at a time. So we can think that if we give a time-consuming task, it will block the javascript runtime and other process will be slow down. But magic is here. Browsers play an interesting game here. Something called an event loop gives the javascript power to manage many tasks at a time. This behavior is called an asynchronous system. Asynchronous means it will not maintain sequence.

How asynchronous works?

Ok, all javascript task goes to call stack first and then executed to do the assigned task. As javascript is a single thread language. It cannot process more than one code at a time in the call stack. So when time-consuming tasks come call stack sends the task to something called web API. In this meantime, web API process the task when the process finished it is sent to the task queue. The tasks waiting in the task queue until the call stack become free. When all the task in the call stack finished. Tasks from the task queue go one by one to the call stack. This is how Javascript is providing the feel of multi-thread despite being a single thread language.

Why setTimeOut with 0 does not execute instantly?

setTimeOut is an asynchronous function that takes a callback function as a first parameter and delays time as a second parameter. In this sense, if we use 0 as a second parameter it should be executed instantly. But here’s some concept to understand. Let’s learn together,

setTimeout(()=>{

console.log(“Hello”);

}, 3000);

So these codes will log “Hello” to the console after 3 sec,

If we add two console log before and after setTimeOut and set the delay time 0, what will happen?

console.log(“I am before set time out”);

setTimeout(()=>{

console.log(“Hello”);

}, 0);

console.log(“I am after set time out”);

Many of us think we will see,

“I am before set time out”

“Hello”

“I am after set time out”

But it’s not true. as setTimeOut is an asynchronous function call stack will send it to web API to be processed. As the delay is 0 second the task will go instantly to the task queue. But the call stack is not free. First, it completed printing “I am before set time out” to the console, and then it sends setTimeOut to the web API. As now call stack is free it will then start working with the last console.log function. And the setTimeout’s callback is still waiting in the task queue. After printing the “I am after set time out” the call stack will be free then the task queue sends the callback to the call stack and lastly “Hello” is printed. So it will be,

“I am before set time out”

“I am after set time out”

“Hello”

Code has style to write??

We often think the styling is a graphic designing thing. But here mainly styles mean the rules. Some rules for writing codes to understand the code easily and to make it looks cleaner. Let’s see some rules in below picture,

Javascript Comment

Comments are the codes or strings that will not execute while compiling. It mainly used to write something in the code which should not be executed.

Single line comment starts with //

And Multiline comment start with /* and ends with */

Example:

//This is a single line comment

/* This is a

multiline comment. */

Explanatory comments are bad things. You don't have to explain every code you write. Comments make the code easily understandable on the other hand if we use so much comment in code it becomes complex.

Cross Browser Testing

When we develop an application or website, generally the main target is to give users to finish their desired task. Here comes the term user experience. We have to make our application fit for the maximum user. As different users use different browsers, we have to make code that will fit in different browsers. Because the different browser is developed differently so all codes don't work perfectly in all browsers. So at the time of development, we test the code in different browsers. This is called cross-browser testing.

--

--