Let’s get ideas about some interesting topics of JavaScript

Shakil Chowdhury
3 min readMay 6, 2021

In this article we will discuss about some interesting topics and methods of javascript.

Lets go through it.

#typeOf()

There are many types of values is javascript. So sometimes its confusing to know about some value types.

Lets get into a example to understand about this : -

console.log(typeOf(4)) //output — “number”console.log(typeOf(‘shakil’) //output — “string”

#Comments

Sometimes its important to do some commenting in code. For big applications when you get into it if there is no comment in the particular module of codes it will be hard to take care of it for a upgrade.

We can do commenting in JavaScript also. Lets focus on the example to understand about JavaScript commenting.

//check prime numberPrime://check prime numbers between 1 to 9for(i=10; i<10; i++){//check if i is a prime number   for (let j=1; j < i; j++) {    if(i%j == 0) continue Prime;   }}

But now a days using // this type of commenting allows you to comment only in one line and that’s is not a good commenting option so that developers use multiline commenting by

/*This will allow you to do multiline commenting*/

#Curly braces

Now a days many of us don’t know about the best variants of curly braces.

Here we will see the best practice of using curly brace. Lets focus on the example.

if(g < 11) {alert(‘{g} is less than 11’)}

Now we will discuss about one of the most useful topic of javascript which is Error Handling.

Every programmers know about errors . Sometimes its hard to get know about the exact error and handle it. So here we will discuss about error handling functionalities in javascript.

#The try….catch syntax

Let get into code first :-

try {//code}catch (error){//error handling}

In the try block we will write our code. And in the catch section we will write the warning if there is any error in the try block. By passing error object in the catch we can get the error if there is any error in the try block. By using this we can do error handling so that the error in code in the try block will not kill the entire script.

#Error object

The error we pass through the catch block is an object. And it has some properties.

It has two main properties for all built in errors.

Lets get some ideas about the properties.

# name (error object property)

By using name we can get the error name . Like for an undefined variable it will return Reference Error.

Lets check out an example if we pass an undefined variable in try block lets see what the error catch block returns.

Example :-

try {Uncle //undefined variable}catch(error) {console.log(error.name);}Output: ReferenceError

#message (property of error object)

The message property of error is a useful property to understand more about the error.

For more understanding lets go through the code and examine the output :-

Example: -

try {notimeforprem} catch (error) {console.log(error.message);}
//output: notimeforprem is not defined

#stack (property of error object)

Sometimes its important to know more about the errors . Mainly stack property used for debugging. If we dive into the example code and analyze the output we will understand more about this property.

try {premKorbona} catch (error) {console.log(error.stack);}//output: ReferenceError: premKorbona is not defined at (…call stack)

So from this section we can get proper ideas about error properties.

#catch binding

if you don’t need to know about the error. But you want to run your code without knowing the error you can use this. The catch block will return nothing if there is error but the code will run.

Example :

try {   lol} catch {console.log(“error caused”)}//output: error caused

#Throw operator

Throw is a special operator for generating an error.

try {   myName = “shakil”if(!userName) {   console.log(“no username is defined”)}} catch (error){}//output: not username is defined.

That’s all about today. Thanks for reading my article.

--

--

Shakil Chowdhury

Web development and Web technologies are my passion from early years. And I am very well determined about my passion.