Tuesday, January 25, 2022

Try/ Catch Wrapper

Hey Guys,

do you also have a lots of Try/ Catch in the code? Of course yes. In my previous project I saw interesting wrapper for that.

Let see it:

Happy codding!

Thursday, January 13, 2022

Arguments in arrow function in JavaScript

Yes, that is true. You canot use the arguments object in arrow function. But if you want to access  the arguments object in an arrow function, you can use the rest parameters feature.

See the example bellow:

Happy coding.

Friday, January 7, 2022

Conditional async import in JavaScript

In this post I am going to talk about conditional import module in JavaScript ( ECMAScript modules ).

The import statemen in JavaScript is static and therefore has two limitation:

  1. a module identifier cannot be constructed at runtime
  2. have to be declared at the top level of file and therefore cannot be nested within control flow statement like if statement.

In case you need conditional import you can use async dynamic import. Async import can be performed at runtime using special import() function.

The module identifier can be any module identifier supported by static import.

Let see example:

This form also supports the await keyword:

Thursday, December 30, 2021

Playing with async function

 Async/ await is cool feature in JavaScript. Much has been written about it. In this post I would like to show how to resolve more independent async functions calls.

Let's have a simple promise call. It takes 1 second.

Now we will call the function multiple times using await. It takes aproximately 10 seconds.

When our asynchronous function is independent, it is useful to use Promise.all() instead of await. It fulfilled all promises in time approximately 1 second.

Thursday, December 23, 2021

Javascript closures and recursion

What is a closure?

A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables.

I think, that closures are very handy for recursive call.

What is a recursion?

Simply said, the process in which a function calls itself.

When you create a recursive function, keep in mind that there must be an escape from the recursive function, otherwise you will get into an infinite loop.

Below I created a function, which call yourself. Recursion can be very complex, but the principle is the same.

Try it yourself:

Sunday, December 5, 2021

Simple accordion on your website

Hey guys! Do you know about details and summary HTML5 tags?

This creates a simple accordion on your website. It si simple and elegant, isn't it?

Example:

JavaScript JavaScript (/ˈdʒɑːvəˌskrɪpt/),[10] often abbreviated as JS, is a programming language that conforms to the ECMAScript specification.[11] JavaScript is high-level, often just-in-time compiled and multi-paradigm. It has dynamic typing, prototype-based object-orientation and first-class functions.
Node Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm,[6] unifying web-application development around a single programming language, rather than different languages for server-side and client-side scripts.

Lookup map in JavaScript

Hey guys! Do you know if statement? Of course yes! But do you know lookup map? It's an elegant way to solve a multiple conditions.

Instead of using if..else, switch, we can define in advance a lookup table of values based on certain key.

Let's look at an example: