10 Random topics in JavaScript
—
1. Spread operator
It’s a handy tool to get the contents out of an array or object. For example you can use it like below to copy an array:
const array = [1, 2, 3]
const copy = [...array];
console.log(copy) // [1, 2, 3]However note that it is a shallow copy. Doing deep copy or clone is not very simple. For that you can take help of tools like lodash.
2. Arrow functions
Arrow functions make programmers life easier. Arrow functions are expressions. Example:
const add = (a, b) => a + b;
console.log(add(1, 3)) // 4Regular functions have this binding. But arrow function don’t have their own this. They take this from closest upper scope.
3. Catching errors
You can catch runtime errors using the try/catch block. Inside try block write code that may throw errors and inside catch block catch error if there is like below:
try { throw 'it is the most exciting task in the world';} catch(error) { console.log(error.message);}
As you can see the error argument is an object provided by JavaScript which has an property called message.
4. Methods of primitive types
You might have heard that primitive types are not objects. But following code works:
"Hello World".toUpperCase(); // HELLO WORLDIf it’s not an object, how does it got a method? It’s actually a primitive type but JavaScript temporarily creates wrapper for providing the method and after providing the method it gets destroyed. An quick little internal magic trick.
5. Linting
It’s not actually thing feature of JavaScript. But it helps programmers to write consistent code. Lint or linter are tools to analyze stylistic error in code. Popular linters are:
- ESLint
- JSLint
- JSHint
Currently ESLint is used most commonly.
6. Hoisting
When declaring a variable with var or writing function statement, JavaScript hoists or raises them to top of current scope. This way you can use variable or function before actually it’s declared in code. This has both advantage and disadvantages.
7. Block level declaration
ES6 offers new keywords let and const to create block level declarations. Now you can:
{
let fruit = 'apple';
}
console.lot(fruit); // error fruit is not defined8. Event Loop
Event loop is a vital cog in the machinery to make JavaScript handle asynchronous tasks smoothly.
Get Ashutosh Biswas’s stories in your inbox
Join Medium for free to get updates from this writer.
It continuously looks for new available tasks and pushes them to the call stack when it’s empty.
9. Default parameters of functions
If we want to a method to have default parameter value, now we can do it. It’s ES6 feature. Below is an example:
function greet(name = "user") {
console.log(`hello ${name}`);
}greet(); // hello user
greet('world'); // hello world
10. Rest parameter
This another ES6 feature. Now you can capture all the remaining arguments in an array. For example:
function greet(...names) {
names.forEach(name => console.log(`hello ${name}`));
}Just make sure that it comes as the last part in your function’s parameter listing.



