JavaScript Fundamentals

Fathima Zihara Iqbal
4 min readFeb 26, 2022

Through this article, you will understand the differences between Java and JavaScript (JS), concept of functions, what is meant by arrow functions and promises in JavaScript.

First let us contrast Java and JavaScript, which are popular programming languages.

The above survey clearly depicts that most developers currently prefer using JavaScript rather than using Java. It is important to know about JavaScript not because it is popular, but because it will enhance our knowledge. Let’s start with the arrow functions in JavaScript.

Arrow Functions in JS

In JS, there are various ways to implement functions. To implement a function, it is required to give the function keyword so that JS understands that it is a function. Then give the function name, pass any parameters within the brackets, and implement the necessary code logic within the curly brackets.

This is the normal way of implementing a function.

In the above example, the return type is not specified, but JS is intelligent enough to automatically detect the return type based on the information that exists inside the code block. The return type can be a string, number, object, or an array. In the same way as in Java, we can call the method using the function name. That is the reason why it is known as a named function.

This is another way of implementing a function.

The above function is known as an anonymous function because there is no name for the function. But the implementation and execution are identical to the previous example. In JS, a function can be assigned to an expression if there is no name to call that function. In this case, the expression is a variable. Then that variable named “calculate” acts like a function.

Above mentioned methods are traditional ways of implementing a function in JS.

With the release of JS ES6 version in 2015, arrow functions were introduced.

There is no keyword called “function” in arrow functions, but instead there is an arrow. There are no programming language level keywords. The prime reason for using arrow functions is because of its simplicity which makes the code more readable.

Implementation and execution of an arrow function are given below.

Output

“printFunction” variable acts as the function. A parameter called “name” is passed to the above arrow function.

Implementing the same functionality in a traditional way:

Output

Promise in JS

A promise has a producing code and a consuming code. Producing code takes some time to execute.

For example, establishing a database connection, it might take some time. This logic is called as producing code.

Consuming code is the code that expects and waits for a result from producing code.

For example, if it is required to insert a student’s information into the database, then the database connection should be established initially. Establishing the database connection can take some time, but the consuming code, which is inserting a student’s information into the database, must wait until the producing code provides some result.

A Promise in JS is a basic object which bridge the gap between producing code and consuming code.

Promise has 2 approaches:

  1. Promise is success — promise has been fulfilled or completed so that promise can be resolved.
  2. Promise is broken — it is required to reject that promise because it is not necessary to maintain that promise when it is broken.

In the promise, a result is expected. It can either be a success or broken.

Implementation of a promise:

Output

That's it for this article, thank you so much for reading. Feel free to express your thoughts in the comment section. Explore more about this topic.

References

https://towardsdev.com/promises-in-javascript-285f523c3e8d

--

--