JavaScript Essentials: Part 5
Previously in JavaScript Essentials: Part 4, We discussed if and else statements, for and while loops. In this part, we will look at:
- Functions
- Callbacks, promises, async & await
- Next big thing
Comments
Comments are great and we are now going to talk about it. It is so late that you should know what a comment is. Anyway, a comment in our program is not executed. A comment is meant to document our code. There are three ways to add comments in Javascript. We have the inline, multiline and JsDoc.
In-line
1 2 3 4 5 6 |
|
Multiline
1 2 3 4 5 6 7 8 9 |
|
JsDoc
1 2 3 4 5 |
|
Comments can be placed anywhere; however, be careful when placing them after (or at the end) a line of code or below or above it.
Semi-colon
In javascript, semi-colon, ;, is not required however, it helps sometimes. There are tools that help you with it. A semi-colon indicates the end of a statement. Good.
Indentation
Indentations are used to arrange code for clarity and ease of reading. The tab key (on the keyboard) is used in indenting. Indentations are sometimes "tabs" or "spaces". The space is usually 2 or 4. If you are using vscode, you don't have to worry.
Examples
There were some exercises from JavaScript Essentials: Part 4 which included but were not limited to "fizzbuzz", password and email validation, etc. If you were to have followed my pseudocode, you'd run into some issues. I will provide a snippet that considers the order.
fizzbuzz for a single number
1 2 3 4 5 6 7 8 9 |
|
fizzbuzz for an array
1 2 3 4 5 6 7 8 9 10 11 |
|
password validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
Some other solutions would be using nested if and else.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
What do you think about the two snippets? Practically the second snippet, even though it works, is not that great.
Functions
A function is a piece of code that can be reused. Usually, a function does a specific thing. One thing. It can be anything.
Let's look at the general form (structure) of a function in JavaScript.
1 2 3 4 5 6 |
|
- function is a keyword required when creating a function. The for keyword is needed when you want to use a for loop.
- functionName is supposed to be the name given to the function. The idea of naming a variable applies to a function.
- /* parameters */ refers to the data you want to pass to the function.
- // do something is the action or computation we desire to be performed. Functions usually return data after some processing is done. There are times when it doesn't. It just updates some data and is done.
- { // do something } is the functions body or block
We can have a function that prints "hello world"
1 2 3 4 5 6 7 8 9 |
|
We did ourselves the favour to name our function with a name that describes what the function does.
Now, when we have a function, we have to "call" it for it to be executed. To call a function, you need the function's name followed by ( and ). If the function takes a parameter, you'd pass the argument in the ( and ). In our case, for the "hello world" function, we have to do, printHelloWorld();.
1 2 3 4 5 |
|
Let's move in a little direction that will broaden our arsenal and make creating functions fun. Consider this function that adds two numbers and then prints a text telling you what happened.
1 2 3 4 5 6 7 8 9 |
|
Is this giving you ideas? We can write our "fizzbuzz" and validations using functions. We can be so stingy and delicate that we'd write each validation requirement as a function. It happens. Just don't overdo it.
Now, consider the add function. What if we want to add different numbers, what do we do? We can create another function that. We can also alter the values directly. Right? Yeah. You will be amazed by what we can accomplish with functions.
First of all, if we want to add different numbers we can change the numbers.
1 2 3 4 5 6 7 8 9 10 11 |
|
Okay, let's alter the function to add 6 and 100 rather. Now we have to alter the function. There is a solution to this and it is to introduce parameters (data via variables). Then we'd pass those data as arguments.
Let's analyze our add function. The add function operates on x and y and operands. We can pass different values to x and y by passing x and y as parameters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
Instead of having the values of x and y as internal values in add, we pass them. Now the difference between parameters and arguments is that parameters are passed when creating (defining) the function. Arguments are the values passed when calling the function. So in function add(x, y), x and y are parameters (we can say placeholders, representing the data to be passed to the function). In add(3, 30);, 3 and 30 are passed as arguments (the actual values to be processed). Note that the order of the argument and parameters must match else we'd be in serious debt.
Do you think it is enough to take on the big guns? Well, I think you can. You just have to be calm and know what you are doing. I will provide some snippets.
1 2 3 4 5 6 |
|
Do the same for the "fizzbuzz". Wrap a function around the snippet. You don't have to comment on the variables used. Look at what data needs to be passed to the function (input).
We can pass as many parameters to a function. However, I'd encourage you to set some limits. There are some professionals who say about three is enough. Some say about fewer than five. You have to be smart about it. For now, let's say that whenever the number of parameters exceeds three, we would use an array or an object. Yeah. We can pass an array or an object as an argument.
1 2 3 4 5 6 7 8 9 |
|
Write a function that calculates the average of an array of numbers by completing this function.
1 2 3 4 5 |
|
At this point, it should be clear that functions can take arguments. Practically, our functions will return a value or something value after a computation is done. The computed value is returned from the function. A function that returns a value is of the form:
1 2 3 4 5 6 7 8 9 |
|
- // return someValue is the only new thing here. return is a keyword.
- someValue is the value returned from the function. And it could be anything to nothing, a void function. Don't sweat it. We will modify some of these functions we have written before so things will be simpler.
Remember the add function? Instead of logging the value inside the function, we will return it and assign that value to a variable then reuse the value later.
1 2 3 4 5 6 7 8 9 10 11 |
|
This is as simple as we can put it. Do the same for the calculateInterest function.
A function can return anything returnable.
Arrow functions
An arrow function is another way to write a function. Usually, I use arrow functions when I have a simple function that does a very minute "thing" or in array or string methods for looping. You can use it in place of the function declarations (named functions). We say, function, to indicate we want to create a function. Arrow functions have the same features as the declarative function.
Arrow functions are called so because of =>, the fat arrow operator. It is of the form, perhaps you've seen before:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Let's rewrite the add function using the arrow function.
1 2 3 |
|
=>, indicates a return of the value from the x y express. So the return keyword is used implicitly. Again we can explicitly return a value from the function using the return keyword however, we have to add the functions block.
1 2 3 4 5 6 |
|
The difference between the two is that in the second, we added a block, { and } and a return keyword that returns a value from the function. Again, you can choose to return a value or not.
Passing functions as arguments
We can pass functions as arguments to other functions. Essentially, we treat functions as values. Let's consider this trivial example.
1 2 3 4 5 6 7 8 9 |
|
Another place we can do this is with array methods or string methods. Consider this function
1 2 3 4 5 |
|
We can see that we can pull out the callback function, (total, element) => total element, 0. In fact, it is the total element we can replace.
1 2 3 4 5 6 7 8 9 |
|
You know we pass another function that takes 2 numbers argument and returns a number. We don't even have to create a function.
We have done some maths before but this time we will use functions to abstract the operators.
1 2 3 4 5 6 7 8 9 10 11 |
|
The last parameter is called a default parameter and usually, it is placed as
the last argument. This is something that you have to do if you are going to
use default values. This snippet is not that different from the previous
one except for the introduction of the default parameter which means for the third
argument, we can choose to pass a value for it or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
In const total = performActionOnArray(numArray, add); we could have passed a function directly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Promise
Before anything, let's define some terms. Promises are important in our niche.
Synchronous operation: These are operations that are executed sequentially, from top to bottom, one after the other. For some operations A1 and A2, A1 has to be completed before A2 will be executed. This way, A2 will not be executed until A1. At a time one operation is executed. This drawback is called blocking.
1 2 3 |
|
The output for the above is in a linear order as written above.
1 2 3 4 |
|
In short, the code we have written so far is all executed in a synchronous order and we can tell when one line will be executed.
Asynchronous operations: These are operations that are not executed sequentially. These operations run concurrently. These could be several operations running at the same time, practically, bit by bit. Since the success or execution of one operation is independent of the order and doesn't impede the execution of other lines, we call this behaviour non-blocking. We can not tell when an asynchronous line will be executed.
1 2 3 |
|
And this is the output.
1 2 3 4 5 6 7 8 9 10 |
|
Can you identify the async operation based on the output?
It is the setTimeout function. Let's say it runs in the background. It is non-blocking, so the last console.log was executed.
Some Async Operations
- Network requests (Eg: API calls)
- Database queries
- File I/O operations
- Javascript Web APIs (setTimeout, setInterval, fetch, etc)
A Promise provides a means for managing or handling asynchronous operations. It is a way of knowing the state an async operation is in, when it is executed, and whether it is "fulfilled" or it failed.
Let's create a Promise
A promise has the form:
1 2 3 4 5 6 |
|
new and Promise are keywords. resolve and reject are callback functions. We can replace them with other names. By conversion, we use resolve and reject.
Handle a promise
Promise has then method which provides the resolved value, catch method which provides the rejected error and there is the finally which is a way to clean up after the whole process. finally is optional though. Here is a simple example you can play with.
1 2 3 4 5 6 7 8 9 |
|
Look at the output and see how the code is executed. console.log("Bye"); was not the last to be executed. We created our async operation using a promise and handled it using then and catch. If we are thinking of executing these operations in order, then we can or have to put the remaining logic inside the then block.
1 2 3 4 5 |
|
What happened?
The issue with this approach to handling promises is that we tend to nest or chain this operation, the then block fattens and it is not that friendly. So let's look at async and await.
async and await
In the normal flow of data, we don't want the async operation to run in the background. We want to listen to it and use its result to do something else (as we did in the then and catch).
Let's create an async operation and handle it using async and await.
We know how to create named functions and arrow functions.
1 2 3 4 5 6 7 8 9 |
|
To make a function asynchronous, we use the async keyword.
1 2 3 4 5 6 7 8 9 10 11 |
|
Now whatever operation goes into the block of the function and let's say in the async function we want to create, we would have to deal with another async operation, then we can use the await.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
The await tells javascript to "wait" and receive the resolved or fulfilled value from the promise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
When we execute the above snippet we get an error similar to, Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension..
We can fix this issue easily. Run the command, npm init -y. Go into the package.json file and add the line, "type": "module". The package.json should look like
1 2 3 4 5 6 |
|
Now, rerun the snippet and you should get an output similar to
1 2 3 4 5 6 7 8 9 |
|
Now, let's say we want to handle the promise rejection error, in case there is no, we have to use the try and catch clause around the async call.
1 2 3 4 5 |
|
There won't be any promise rejection because we set, const result = true. Set it to false. And our output should be similar to
1 2 3 4 5 6 7 8 9 |
|
So the purpose of talking about promises and async and await is to let you know that we will be doing that a lot. Refer to the examples of asynchronous operations listed above.
async and await, try and catch and finally are keywords.
Conclusion
At this point where we have discussed functions and promises and how to handle them, I think we are about 50% equipped with the "knowledge" to manipulate data (flow). What is left is to become used to writing javascript code and be a competent javascript programmer. Get your hands dirty with JavaScript. That's the only way you will code a backend API and not feel restrained because you have the idea but don't know what to do.
Next is to write some code and solve some problems then actually start building APIs. That is what we will be doing next, however, I will encourage you to check out the resources below.
Resource
These are materials that will be helpful in understanding Promises, async and await and event loop.
- What the heck is the event loop anyway? | Philip Roberts | JSConf EU
- Node.js animated: Event Loop
- JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
- JavaScript Promises In 10 Minutes
- How JavaScript Promises Work – Tutorial for Beginners
These are some exercises you'd like to try your hands on.
- exercism - javascript
- jschallenger - javascript-basics
- codecademy - 10 javascript-code-challenges-for-beginners
- TheOdinProject-javascript-exercises
- codewars - javascript
- RepublicServicesRepository-javascript-exercises
- leonardomso - 33-js-concepts
- This is a good read, getify - You-Dont-Know-JS
- TheAlgorithms - Javascript
- denysdovhan - wtfjs
- w3schools - js exercise
The above is the detailed content of JavaScript Essentials: Part 5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.
