Some Important topics of JavaScript

Md. Alamgir Hossain
4 min readNov 3, 2020

What is a comment in JavaScript?

=> JavaScript comments are used to explain JavaScript code, and to make it more readable. These codes are compiled by the compiler. There are mainly two types of JavaScript comments.

  1. Single Line Comment: Single line comments start with //. Any text between // and the end of the line will be ignored by JavaScript (will not be executed).
  2. Multi-Line comment: Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored by JavaScript.

What is try-catch in JavaScript?

When executing JavaScript code, different errors can occur. Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things. The try statement lets you test a block of code for errors. The catch statement lets you handle the error. The throw statement lets you create custom errors. The final statement lets you execute code, after try and catch, regardless of the result. Syntax:

try {
//Block of code to try
}
catch(err) {
//Block of code to handle errors
}

What is Hoisting in JavaScript?

Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope. In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.

carName = “Volvo”;
alert(carname);
let carName;

Variables defined with let and const are hoisted to the top of the block, but not initialized.

Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared. Using a let or const variable before it is declared will result in a ReferenceError. To avoid bugs, always declare all variables at the beginning of every scope.

What is Block in JavaScript?

A block statement (or compound statement in other languages) is used to group zero or more statements. The block is delimited by a pair of braces (“curly brackets”) and may optionally be labeled. There are some rules about the declaration of a variable in the scope JavaScript. Variables declared with var or created by function declarations in non-strict mode do not have block scope. So, it can be accessed from anywhere of the code. But,
By contrast, identifiers declared with let and const do have block scope. So, It will only be accessible from the block where it was declared. We need to declare them globally to use it from anywhere. Example-

var x = 1;

let y = 1;

if (true) {

var x = 2;

let y = 2;

}

console.log(x);

// expected output: 2

console.log(y);

// expected output: 1

Block Binding in Loop in JavaScript

As we know from my previous post, that when we declare a variable with var, it will be hoisted to the top of the function. So even if we declare the variable within a for loop, we can access it from outside of the loop as well.

for(var i = 0; i < 5; i++) {

console.log(i);

}

// we can still use i, outside the for loop

console.log(i);

In other languages where the Block level is default scope, then I variable will be accessible only in the for loop and will not be accessible from outside. So to fix this in JavaScript, ES6 has introduced the let & const datatypes.

for(let i = 0; i < 5; i++) {

console.log(i);

}

// i is not accessible outside of the loop and will throw an error

console.log(i);

Default parameters in JavaScript

In JavaScript, function parameters default to undefined. However, it’s often useful to set a different default value. This is where default parameters can help. If we set a default value of a parameter in a function, it will use that value of the parameter when the value was not declared in the callback. Example -

function multiply(a, b = 1) {

return a * b

}

multiply(5, 2) // 10

multiply(5) // 5

multiply(5, undefined) // 5

The spread operator in JavaScript

Spread syntax (…) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected. With spread syntax push(), splice(), concat() becomes much more succinct. Like -

let parts = [‘shoulders’, ‘knees’];

let lyrics = [‘head’, …parts, ‘and’, ‘toes’];

//[“head”, “shoulders”, “knees”, “and”, “toes”]

Arrow function in JavaScript

Arrow functions allow us to write shorter function syntax. It gets shorter! If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword. If you have parameters, you pass them inside the parentheses. In fact, if you have only one parameter, you can skip the parentheses as well. Example-

const hello = val => “Hello “ + val;

Thanks for reading this.

Alamgir

--

--