Javascript is one of the most popular languages for web development. It’s easy to learn and yet, it has a lot of powerful features that can make your work easier. In this article, we will share 7 crazy js features that you must know about!
Async/await is a new feature in ES2017, and it allows you to write asynchronous code that reads like synchronous code. It makes it easier to write error-handling code because you don’t have to deal with the complexity of dealing with callback functions and promises.
Async/await is the combination of two features that make asynchronous programming much easier to understand. It allows you to write asynchronous code in a synchronous manner, making it much easier to read and understand your code.
Asynchronous programming is a way to do multiple things at the same time. It allows us to write code that looks synchronous but works asynchronously. You can use async/await in Node.js or with transpilers like Babel.
You can use async/await with callbacks, promises and generators too!
For example:
async function asyncExample(callback) { let result = await promise; callback(result); } asyncExample(function (msg) { console.log(“Here I am”); });
Object.assign() is a way of adding or modifying properties on an object, without affecting the prototype chain. It’s similar to setter methods in that it can only be used with objects that have been created with new ones.
It works by creating a brand new object and assigning the value of the existing one to its properties while making sure that no modifications are made on any other part of the object (such as names or prototypes).
For example: if you want to create an array containing all of your movies’ titles and release dates in JavaScript, you could use this method instead:
Block scope variable declarations are variables that are declared inside a block, and only visible to the code in that block. For example, you can create a variable like this:
“`javascript var name = “Taha”; //variable has global scope (can be accessed from anywhere) “`
The above code declares a variable called name with global scope. This means that it will be accessible anywhere in your application including in other functions/libraries or even on another page if you decided to use jQuery or any other external library as well! However, this kind of behavior can be dangerous as it allows developers to overwrite each other’s variables without knowing about them which may lead to serious issues later on down the line when they encounter bugs within their code base which we’ll discuss later on
Rest parameters are an ES6 feature that allows you to pass in an array of arguments to a function. With rest parameter syntax, you can use any number of arguments:
function foo(first, second) { // first and second are separated by commas } foo(1, 2); // will output: 1 2
How it works? The first argument is the callback function’s name; after that comes another set of parentheses and all remaining arguments (if there are any). So if you want your callback to accept two numbers as its first two parameters, then simply call it like this:
function foo(firstNumber = 1) { // Passing 1 as your second parameter here will ensure that your third parameter isn’t accidentally overwritten when someone calls this method multiple times without providing enough information about what they want it for!}
Arrow functions are syntax for writing functions that are more concise, readable, and easier to use than the traditional function definition syntax.
Arrow functions have three main advantages:
Promises are a way to handle asynchronous code. They’re used in async/await, but you can use them without it, too.
Promises are objects that represent a value that may or may not become available at some point in the future. They have two attributes: then and done . The then attribute contains the code to be executed when the promise becomes available; this is called “fulfilling” it. The done attribute contains code that will be run when no more work needs to happen — in other words, this is what gets done once all of your asynchronous tasks are complete (or if they fail).
Here’s an example of using promises:
“`javascript var p = new Promise((resolve) => { setTimeout(resolve); }); p(‘a’, ‘b’).then(function(){ console.log(‘done!’); }, function(){ console.log(‘failure’); }); “`
Classes are a way to define a constructor function in JavaScript. It can be used to create an object, which is defined as follows:
// A class definition
class HelloWorld {
constructor() { console.log(“Hello World!”); } }
JavaScript has come a long way from its humble beginnings. In this article, we’ve explored some of the crazy features that help make JavaScript what it is today. We hope you learned something new! We recommend checking out our website or try contacting us if you want to learn more!
Read more about JS and other programming languages in the following blogs:
published: December 6, 2022
© Copyright 2023 Cognitionteam All Rights Reserved