DEV Community

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

Posted on

3 1

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.

let a;
let b = null;
if (a === undefined) {
console.log("It is undefined, as it is not assigned any value.");
} else
console.log("It is not undefined");
if (b === null) {
console.log("The value is null, since it was the one assigned.")
} else
console.log("It is not null")
view raw example.js hosted with ❤ by GitHub

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.

function Readfile(filename, sizefinder) {
console.log('Reading file.....');
sizefinder();
}
function sizefinder() {
console.log('File size is 32mb');
}
Readfile('sampletxt', sizefinder);
view raw Callback.js hosted with ❤ by GitHub

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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

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 ❤️

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay