DEV Community

Cover image for Every Developer Must Know These 7 JavaScript Concepts.
Nathan Orris
Nathan Orris

Posted on

Every Developer Must Know These 7 JavaScript Concepts.

I made a goal for 2022 to master the fundamentals.

Remember being a kid and building sand castles at the beach? Only to come back after a few hours and see the whole thing being washed away? The issue was with the foundation. Turns out sand doesn't really stick together for long, particularly if you add water. This is exactly what most developers are trying to do these days and at times I have found myself guilty as charged... Learning framework after framework, constantly adding new things on top of weak foundations. But, no matter how tall the castle is, if the foundations are built of sand, they will be washed away in a matter of seconds.

No matter how many new shiny JavaScript frameworks we will see out there, the foundational piece will still be a JavaScript bundle that gets shipped through HTTP to a browser.

1. Scope

Scope means variable access. What variable do I have access to when a code is running? In Javascript by default, you’re always in root scope i.e. the window scope. The scope is simply a box with a boundary for variables, functions, and objects. These boundaries put restrictions on variables and determine whether you have access to the variable or not. It limits the visibility or availability of a variable to the other parts of the code. You must have a clear understanding of this concept because it helps you to separate logic in your code and also improves the readability. A scope can be defined in two ways –

  • Local Scope allows access to everything within the boundaries (inside the box)
  • Global Scope is everything outside the boundaries (outside the box). A global scope can not access a variable defined in local scope because it is enclosed from the outer world, except if you return it.

Example: The code given below will give you an error because “name” is defined within the boundary (local scope) of the showName() function. You can not have access to this variable outside the function.

Undefined Example Code

Now, take a look at the code given below and see how you can access the variable “name” defined in the local scope.

Correct use of local scope example

2. IIFE (Immediately Invoked Function Expression)

As the name suggests IIFE is a function in Javascript which immediately invoked and executed as soon as it is defined. Variables declared within the IIFE cannot be accessed by the outside world and this way you can avoid the global scope from getting polluted. So the primary reason to use IIFE is to immediately execute the code and obtain data privacy.

3. Hoisting

A lot of developers get unexpected results when they are not clear with the concept of hoisting in Javascript. In Javascript, you can call a function before it is defined and you won’t get an error ‘Uncaught ReferenceError’. The reason behind this is hoisting where the Javascript interpreter always moves the variables and function declaration to the top of the current scope (function scope or global scope) before the code execution. Let’s understand this with an example.

Example: Take a look at the code below.

Declare and then invoke function

Now what happens if we invoke our function before we declare it (with hoisting)

Invoke and then declare function

The above code is not giving an error and you get the output ‘A-oo-oo-oo-ooo! Woo-oo-oo-ooo!’ in your console. This is hoisting in javascript.

4. Closure

A closure is simply a function inside another function that has access to the outer function variable. Now, this definition sound pretty much straightforward but the real magic is created with the scope. The inner function (closure) can access the variable defined in its scope (variables defined between its curly brackets), in the scope of its parent function, and the global variables. Now here you need to remember that the outer function can not have access to the inner function variable (we have already discussed this in the scope concept). To sum it up: Closures can be defined in simple terms as “a function run, the function executed. It’s never going to execute again but it’s going to remember that there are references to those variables so the child scope always has access to the parent scope.”

5. Callbacks

In javascript, a callback is simply a function that is passed to another function as a parameter and is invoked or executed inside the other function. Here a function needs to wait for another function to execute or return a value and this makes the chain of the functionalities (when X is completed, then Y is executed, and it goes on.). This is the reason callback is generally used in the asynchronous operation of javascript to provide the synchronous capability.

6. Promises

We understand the concept of callback but what will happen if your code will have callbacks within callbacks within callbacks and it goes on. Well, this recursive structure of callback is called ‘callback hell’ and promises to help to solve this kind of issue. Promises are useful in asynchronous javascript operations when we need to execute two or more back-to-back operations (or chaining callback), where each subsequent function starts when the previous one is completed. A promise is an object that may produce a single value sometime in the future, either a resolved value or a reason that it’s not resolved (rejected). According to developer.mozilla_ “A Promise is an object representing the eventual completion or failure of an asynchronous operation. Essentially, a promise is a returned object to which you attach callbacks, instead of passing callbacks into a function.”._ Promises resolve the issue of ‘callback hell’ which is nothing but a recursive structure of callbacks (callbacks within callbacks within callbacks and so forth).

A promise may be in three possible states…

  • Fulfilled: When the operation is completed successfully.
  • Rejected: When the operation is failed.
  • Pending: initial state, neither fulfilled nor rejected.

7. Async & Await

Stop and wait until something is resolved. Async & await just syntactic sugar on top of Promises and like promises it also provides a way to maintain asynchronous operation more synchronously. So in javascript asynchronous operations can be handled in various versions…

  • ES5 -> Callback
  • ES6 -> Promise
  • ES7 -> async & await

You can use Async/Await to perform the Rest API request where you want the data to fully load before pushing it to the view. For Nodejs and browser programmers async/await is a great syntactic improvement. It helps the developer to implement functional programming in javascript and it also increases the code readability.

Example: Check out the below code.

Async and Await Example Code
To notify JS that we are working with promises we need to wrap ‘await’ inside an ‘async’ function. In the above example, we (a)wait for two things: response and posts. Before we can convert the response to JSON format, we need to make sure we have the response fetched, otherwise we can end up converting a response that is not there yet, which will most likely prompt an error.

I hope this helped you in some way. This leads me into a question:

What other concepts are a must know for any Javascript developer?

Top comments (8)

getsetgopi profile image

Nice article. Trust me there is no end to learning and I'm still learning everyday.

  1. OOPS
  2. Factory Functions
  3. proto/prototypal
  4. THIS
  5. Arrow vs regular functions
  6. Higher Order Functions
  7. Value vs Reference
  8. JS modules/patterns
nathannosudo profile image
Nathan Orris

I agree 100%, we as developers are life long learners.

nelcapetown profile image
Nel Prinsloo • Edited

Excellent article and one that I will bookmark. We are indeed constantly learning. Sometimes - as I'm just double checking the syntax of a command or some other fact, I catch myself doing one of the following two things which I know are not optimal:

  • Going down a rabbit hole when I come upon something really interesting. I'll go: "Wow! Look! A squirrel..." and before long I catch myself reading about very interesting and important new tech, although it is at best tangentially related to my current task.

  • Other times I will bookmark the new info in my ever expanding labyrinthene hierarchy of bookmarks. I have become a bit of a hoarder when it comes to bookmarks and have branched out to using additional systems (like Notion) to document and organising the bookmarks and track the rate at which I'm consuming all the new information.

Going back to the basics is indeed excellent advice. It is a great time to be working if you are a developer who loves technology. But ensuring you always retain the basics in shiny, ever-ready-for-action state is becoming more important than ever.

nathannosudo profile image
Nathan Orris

Thanks! I couldn't agree with you any more. I am guilty of getting sucked deeper into the rabbit hole more often than not. There is an incredible amount of interesting information out there which indeed makes now an amazing time to be a developer who loves technology.

yongchanghe profile image
Yongchang He

Nice article, and thank you for sharing!

nathannosudo profile image
Nathan Orris

Thank you!

renancferro profile image
Renan Ferro

Nice article friend!

nathannosudo profile image
Nathan Orris

Thank you, I'm glad you enjoyed it!