An Elaboration on Modern JavaScript Data Types and Discrepancies

JavaScript is a single-threaded, non-blocking, asynchronous, and object-oriented programming language. A couple of years ago JavaScript was used for developing the client-side of a website. But nowadays using JavaScript we can develop server-side also. Like other programming languages, JavaScript also has data types. So let’s dive into the JS data type’s world and also know some discrepancies.

Data Types

There are seven data types in JS. Number, String, symbol, Boolean, object, null and undefined. JavaScript data types can be categorized into two types, primitive and non-primitive data types. Let’s explore both different data types of JavaScript.

Primitive Data Types

Usually, number, string, symbol, Boolean, null and undefined are known as primitive data types. This means these data types are already predefined and cannot be mutable. These data types can only be referred to as a single value. It cannot contains multiple values in the code. Let’s clarify the concept with a simple example

In the example we can see when we create a variable with assigning a value, the variable reserved a space in the memory where it holds the value which is assigned to it.

And when we reassigned the variable with a new value, it does not override or change its memory reference rather the variable reserved a new space in the memory for the new value.

And when we check both variables with triple equal with the same value, it returns true. According to the result, we understood that when we check variables that are primitive data types, it checks the assignment value, not its memory location or address.

Non-Primitive Data Type

There is only one non-primitive data type in JavaScript which is called object. The non-primitive data type is mutable. When we create an object it will create a reference/ address of a single or multiple key-value pair in the memory. And if we create another object and assign the previous object to it as a value then it grabs the address of that variable which is assigned as a value of it. If we see an example

When we check both variables it returns true because both of the objects contain the same address of memory.

Let’s see another example

From the picture we can see when we declare the objects separately with the same key-value pair and checking them with triple equal it returns false. But why! Both variables have the same value. What’s wrong then?????

Hmm, the answer is when we check the variables of non-primitive data types, it will check the memory address or reference of those variables not the key-value pairs of the objects.

The JavaScript array and function are also objects

Now let’s talk about the primitive and non-primitive data types of JavaScript

Number

JavaScript number type can be used in various forms like integer, floating-point number exponential, and NaN. And a variable value can be assigned with Number() function instead of .assigning with a traditional way, Not only that the number data type can be used as an object! See how cool that is

In the image, we can also see that NaN is a number type and can be used to check whether a value is non-numeric. And when we try to divide a value that is greater than 0 and trying to divide by 0 it will return infinity.

String

A string can be a group of characters or a single character enclosed by a single, double quotation. But in ES6 we can write strings using template literals. The benefits of using it we can change value dynamically in the string. And like number type we can create a String object using a new operator and String constructor.

See the Example

Symbol

The symbol is a new data type in es6. Normally symbol is used to define a property of an object which is ideally kept private. Another reason for using the symbol data type is to hide the implementation details of an object. It works as a key of key-value pair of an object. If we can add a property using symbol data type we can do this using Symbol() function. Remember that the symbol data type cannot be used as a constructor. Let’s have a look at the example

Boolean

The data type normally uses to check a logical condition. It has two values true and false. It returns a value based on condition if the condition met successfully it will return true else false. We can also check the type of true and false. Let’s see an example

Null

Null is also a data type in JavaScript. It is mostly used as a value to define no value or empty value. But there is a twist by assigning null as a value of a variable. If we check the type of a variable which value is assigned to null, we will get its type is an object. But we know that null is also a data type itself. This is an error in JavaScript. Let’s see an example.

Undefined

Data type undefined means the variable is declared but there is no value assigned to the variable. It also defines the absence of value. But undefined can occur in various cases such as

· If we don’t return anything from a function based on a function statement.

· If we declare a variable but didn’t assign a value.

· Call a function with insufficient parameter value then it will return undefined.

· If we assign an object property value to a variable that is not in the object then it will return undefined.

Let’s see an example

Without those points, there are so many reasons to return undefined.

Difference between null and undefined

Null and defined both are used to define the absence of values or empty values. let differentiate them

  • Null is used as an ideal value.
  • Undefined can also be used as a value but it is bad practice
  • Null itself is a value with null which defines no value
  • Undefined is normally occur when its value is not assigned yet
  • A variable with no value simply means that it is undefined
  • A variable with no value didn’t mean its value is null
  • The data type of a variable which doesn’t hold a value ‘Undefined’
  • But the data type of a variable contains a value null. Which is an object instead of null.

The Non-Primitive Data type — Object

As we can see before, when we create an object, it reserves an address or reference in the memory location and it’s mutable. Arrays and Functions in JavaScript also are object data types. Let’s see below

Array

An array is a collection of data or elements. The elements can be numeric data, objects, etc. Array maintains an index for each element in the array. So it contains its value in a key-value pair like an object. But remember that we cannot mutate an array. Let see an example

From the image, we understood that the address in the memory of an array or object keeps immutable but it holds its address and now it contains the new values as you can see in the picture.

Function

Ideally, a function is also an object data type in JavaScript. JavaScript doesn’t have a function data type but unfortunately when will we check the data type of a function in JavaScript, we will get a function as a data type but ideally, it was supposed to return an object. This is also an error in JavaScript. Let’s see an example

Like function and array JavaScript also has two more object data types which are date and regex.

Some Discrepancies of Modern JavaScript

Like other high-level programming languages, JavaScript also has some discrepancies. Let’s see

  • Null is a data type in JavaScript and if we check the data type using typeof operator, ideally it is supposed to return null instead of returning an object.
  • A function is also an object data type and like a null data type if we find the data type of a function it should return the object data type but it returns a function.
  • We cannot say that JavaScript is fully an OOP language because it is not a multi-threaded programming language, it is a single-threaded programming language.
  • Lack of knowing the correct usage of null and undefined data type

Conclusion

JavaScript nowadays is a most powerful programming language that easily competes with other high-level languages, and hopefully, JavaScript will come back as an error and bugs free programming language soon. Happy JavaScript error-free coding.

Thanks to all of you to read this article. Please give me feedback to improve my writing skills.

I love programming, always like to do something creative. I have completed my graduation in B.sc in CSE, my dream is to become a successful web developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store