DEV Community

Cover image for Just JavaScript things…
Vishwa.R
Vishwa.R

Posted on

Just JavaScript things…

Hello again, my dear readers and followers👋. Here I am back with another blog on JavaScript. This time it's going to be much more like a knowledge sharing than a technical thing. So, let's begin with today's topic, “Just JS things”.

We are going to discuss some peculiar features of JavaScript which, most of us, don't know. These peculiar things make JS a great language to learn, and for me, it's the most fun thing to do. So, let's BEGIN

undefined and null :

Most of us would have come across the JS data typesundefined and null. But we don't know the real difference between both of them. Let's start with undefined,

undefined :

The undefined type is an object, which represents that the declaration of the variable done, but it is not assigned. This comes under the undefined, as its name suggests. This is literally lack of value for the variable.

null :

null is a value assigned to a variable. Unlike undefined it's not the lack of value, as we know that null by itself is a value. null is voluntary absence of the value for the variable.

The below picture clearly explains the difference.

Example

We'll see how they compare with each other in the below gist, where we use a simple conditional statement to know how undefined and null work.

Note: Line numbers referred as L below.

Here, we only get to run L4 and L8 in our code. Which means that the variable a is not assigned a value and thus gives undefined, whereas variable b is assigned the value of null which make the L8 to execute.

You can also use this JSFiddle https://jsfiddle.net/Vishwa_R/ha8tqL69/5/ for execution.

First class citizens, FUNCTIONS!

In the JavaScript world, functions enjoy many privileges as first class objects. We can pass one function as an argument for another function and can also return the same if needed for later execution. YES! That's possible in JS. These are called as “Callback functions”. They are commonly used in JS world. We use callback functions in asynchronous programming, to wait for execution until a previous function gets its job done.

Let's see a simple example, let us take the operation of reading a file and displaying its size. Here we have two functions to perform, they are,

  1. Reading a file.
  2. Displaying size.

This must be done in sequence, we cannot display the size first without reading the file. Scenarios like this, make Callback functions “HEROES”.

We'll see an example where we mimic the above operation (we are not going to actually read a file and display the size). Let us take a look at the below gist.

So here in this example, we have two functions, namely Readfile and sizefinder. As per our sequence of execution, we want Readfile to be first executed, So, we call the sizefinder inside the Readfile function as an argument. Finally, we can asynchronously do two functions using callbacks. This makes Callback functions to be widely used.

You can also use this JSFiddle https://jsfiddle.net/Vishwa_R/hce58f39/9/ to have a look at execution.

And that's it for today, I think these two things are great in JavaScript and that's why folks like us LOVE JS 📜✨. JavaScript dominates all the possible domains of technology, from Web to Native (A big thanks to NodeJS), and reigns as the most famous programming language. Let us love JS, as we all do every time.

Thanks for reading and give a 💖 if you liked the content, have some feedbacks? Put them down in the comments. Have a great time😄🎉

Attributions:

Cover image : https://wallpaperaccess.com/javascript

Top comments (1)

Collapse
 
codereaper08 profile image
Vishwa.R

That's really insightful @lukeshiru
It makes sense to use undefined Instead of null, as null makes the identification of variables lacking. Which is bad :-\

And thanks for commenting, hope you liked it ❤️