title

7 Cool JS Features You Must Know to Find In Javascript

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!

1. async/await

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”); });

2. Object.assign()

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:

  • var myMovies = new Object(); // Create empty array
  • for (var i = 0; i < myMovies[0].length; ++i) { // Add each title here } * var myReleaseDates = new Array(myMovies[0]); // Initialize release dates

3. Block scope variable declarations

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

4. Rest Parameters

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!}

5. Arrow functions

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:

  • They can be used as constructors in object literals.
  • They take arguments with binding (this).
  • They can bind local variables using let or const keywords

6. Promises in JavaScript

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’); }); “`

7. Classes in JavaScript

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!”); } }

  1. You can then instantiate this class with the new keyword like so: var helloWorld = new HelloWorld(); However, there are some limitations with classes and constructors: 
  2. You cannot add members or methods after instantiating a class;
  3. If a member is ever added or removed from your custom object (for example by using setters), it will break all instances of that type across all browsers;
  4. There may not be any support for inheritance in future versions of JavaScript (though I believe it exists today).

Learn these crazy js features

  • Learn async/await
  • Learn object.assign()
  • Learn block scope variable declarations (with a function)
  • Learn rest parameters in arrow functions
  • Promise chaining with then() and catch().
  • Classes are not only for objects but can also be used on functions and methods as well!

Conclusion

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:

  • Flutter vs React Native
  • Java vs JavaScript
  • React vs React Native
  • Next Js vs React Js
  • React Native SVG
  • 5 Core JS using Frameworks
  • Java Composite Data Types
  • Interface in JS and Importing in JS
  • 7 Cool JS Features
avatar

Cognitionteam

published: December 6, 2022