Better Programming

Advice for programmers.

Follow publication

6 Lesser-Known JavaScript Features

Step up your JavaScript knowledge in just a few minutes

David Jöch
Better Programming
Published in
3 min readApr 5, 2021

--

Closed box
Photo by Jouwen Wang on Unsplash.

JavaScript is one of the most used programming languages today. You can build almost anything with it, like websites, APIs with Node.js, or apps with React Native.

The fundamentals of JavaScript can be learned quite fast, but mastering the language is challenging! There are many parts of the language that are not well-known yet, so I want to share six features with you that you have probably never heard about.

1. The arguments Object

Did you know every function you define in JavaScript has access to the arguments object that contains every parameter you have passed to the function?

This object is similar to an array. For instance, it has a length property, but you cannot use standard array functions like map() or forEach() since it’s actually not a real array. If you want to use these functions on the arguments object, you can convert it to an array like this: Array.from(arguments).

Now, you may ask yourself why you would ever use the arguments object instead of just defining parameters in a function. The best use case for arguments are functions with an unlimited amount of possible parameters.

Let’s take a look at an example:

Another great use case is wanting to validate if you passed the correct amount of parameters to a function:

2. The with Statement

You have probably never heard of this statement, but don’t worry: You didn’t miss a lot. The use of with is actually not encouraged since it can be a source of confusing bugs, compatibility issues, and performance problems.

In the documentation by MDN for the with statement, Mozilla states:

“Using with is not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.”

Nevertheless, let’s take a look at the following example to understand how with works:

I highly recommend checking out this Stack Overflow thread where people explore different use cases for with and also explain why it’s generally a bad idea to use it:

3. The in Operator

This operator is quite handy when you want to check if a property exists on a given object or its prototype chain. Let’s take a look at how you would use the in operator in your code:

It's very important to know that in will also return true for inherited properties, as you can see in the following example:

If you want to check only for non-inherited properties, you can use Object.prototype.hasOwnProperty() instead.

4. The Comma Operator

Another operator that is not well-known is the comma operator. This operator evaluates each expression from left to right and returns the value of the last expression, as you can see in the following example:

A common use case of the comma operator can be found in for loops:

The comma operator helps you to reduce code and can also be a handy utility to run a console.log() in between two expressions:

5. The debugger Statement

Instead of setting your breakpoints manually, you can also use the debugger statement in your code! If there is debugging functionality available (e.g. when you have your dev tools opened), the debugger will stop at this breakpoint. Otherwise, this statement has no effect on your code.

6. Functions Can Have Properties

Since functions are first-class citizens in JavaScript, you can add properties to them. You can use this to avoid global variables when a property on a function would be sufficient:

Conclusion

Thanks for reading this article! I hope you learned something new about JavaScript today. Feel free to ask any questions in the comments section below.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

David Jöch
David Jöch

Written by David Jöch

Freelance Web Developer and Indie App Developer. More about my work on https://joech.io

Responses (1)

Write a response