JavaScript Data Types

M.V. Thanoshan
Oct 2 · 4 min read

Data types basically specify what kind of data can be stored and manipulated within a program.

JS is untyped language. This means that a JavaScript variable can hold a value of any data type. It does not work like C, C++, Java; datatypes like int, char are not needed here.

JS is both dynamically typed and weakly typed. Learn more on JavaScript here.

Photo by NESA by Makers on Unsplash

Statically Typed

JavaScript is not statically typed unless you’re using a language, tool such as Typescript or Flow that compiles to JavaScript code.

Statically typed means the type is enforced and won’t change so easily. All variables must be declared with a type.

int x = 5
string y = ‘abc’

Dynamically Typed

It infer variable types at run-time. This means once your code is run the compiler/interpreter will see your variable and its value then decide what type it is.

var a = 1; // number

Any variable can be assigned (and re-assigned) values of all types.

var x; // Now x is undefined
x = 5; // Now x is a Number
x = “John”; // Now x is a String

Weakly Typed

Weakly typed languages allow types to be inferred as another type.

For example, 1 + '2' // '12' In JS it sees you’re trying to add a number with a string — an invalid operation — so it coerces your number into a string and results in the string ‘12’.

var x = 16 + “Volvo”; //16Volvo
var x = "Volvo" + 16; //Volvo16
var x = 16 + 4 + "Volvo"; //20Volvo

Primitives

A primitive is not an object and has no methods of its own. All primitives are immutable (values, which are incapable of being changed). These seven types are considered to be primitives.

  • Boolean -Booleans can only have two values: true or false.
  • Null -In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null.
  • Undefined -In JavaScript, a variable without a value, has the value undefined.
  • Number -Numbers can be written with, or without decimals.
  • BigInt -The BigInt type is a numeric primitive in JavaScript that can represent integers with arbitrary precision.
  • String -Strings are written with quotes. You can use single or double quotes.
  • Symbol -A Symbol is a unique and immutable primitive value and may be used as the key of an Object property

and Object -Everything else is in Object type.

Objects

There are two that are the main ones you’ll use for your own structures:

There are many other objects too just to list a few:

  • Function
  • Boolean
  • Symbol
  • Error
  • Number
  • String
  • RegExp
  • Math
  • Set

Notice some of these were on the primitive list too, but don’t confuse them. These act as constructors to create those types. Example:

Boolean('a') // true

In JavaScript arrays are objects.

Important Concepts that you should know

Photo by Artem Maltsev on Unsplash

The typeof Operator

The typeof operator returns the type of a variable or an expression.

typeof operator: TutorialRepublic

Difference Between Undefined and Null

undefined and null are equal in value but different in type.

typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true

Why is Null an Object?

If you see the above example, null typeof returns ‘object’. According to documentation It should have returned null!.

What went wrong?

Basically, this is a bug that isn’t fixed because it would break existing code. This bug has been around since the first version of JavaScript.

Read more about the rejected fix proposal here

Why NaN is a number?

typeof NaN; // number

NaN is the result of some mathematical operations that can’t be quantified as a number but NaN can be defined as a numeric type.

A primitive is not an object and has no methods of its own

“You said primitives have no methods but then explain how 'abc'.length works!”

This was not a mistake and that comes from the documentation itself.

First, do not confuse constructors with primitives — every primitive has a constructor or parent object.

JS knows when you’re trying to access a method on a primitive and behind the scenes, it will use the constructor to make an object out of your primitive.

Once it runs the method that object is then garbage collected. (Removed from memory)

Conclusion

Reference: w3schools.com ,MDN Web Docs, TutorialRepublic-JS DataTypes and JavaScript.info

Thanks for reading! Leave any questions/feedback in the comments.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade