5 important concepts when learning advanced JavaScript

Tram Ho

Currying

Currying means evaluating functions with multiple arguments and splitting them into a sequence of functions with a single argument. So instead of taking all arguments at once, the function takes the first argument and returns a new function, which takes the second argument and returns a new function, which takes the third argument … and so on until all arguments are provided and the final function is executed .

Currying helps you break down functions into smaller reusable functions to handle a single task. This makes your functions purer, less error prone, and easier to test.

Simple Currying Example

In the above example, we have implemented a simple Curry logic to handle a function that takes exactly three parameters. The knowledge and examples I give are only for explaining concepts. In fact if you want to apply it I recommend you to use Ramda or similar libraries that support currying functions with any number of arguments and also support changing the order of arguments using use placeholders . However, sometimes the logic is simple, I also implement myself a function like the above, which is convenient and saves a lot of Library.

Composition

Composition is a technique where the result of one function is passed to the next function, the function is passed to the next function, etc., until the last function is executed and the result is calculated. Compositions functions can include any number of functions.

Composition also helps to divide functions into smaller reusable functions that are responsible for handling a single logic. (Dividing and conquering is too reasonable, isn’t it?)

Ramda also has APIs for Composition function with Pipe and Compose .

Closures

Closures are a function that maintains access to the outer function’s variables and arguments (scope), even after the outer function has finished executing. Closures are useful for hiding implement details in JavaScript. In other words, it can be useful to create private variables or functions like this: (It’s almost as close to OOP closure)

Nullish operator ??

The nullish operator is a way to quickly apply a default value if the left operand is null or undefined. This is especially useful in case you want to accept all falsy values ​​other than null and undefined . Or in case you want to apply falsy values ​​as default.

Reflect API

Reflect programmatically means that a program can test itself by interpolating and manipulating its own structures. The Reflect API provides a set of functions useful for both interpolation and manipulation through the Reflect API ‘s static functions.

There are many more that you can read in detail here .

Roundup

As always, I hope you enjoyed this article and learned something new.

Thank you and see you in the next posts!

If you find this blog interesting, please give me a like and subscribe to support me. Thank you.

Ref

Share the news now

Source : Viblo