DEV Community

Alok Kumar
Alok Kumar

Posted on β€’ Edited on

4 1

JavaScript Types

Hey All πŸ‘‹

This is more of a notes rather than an article that I took while doing an course. In this article we will talk about JavaScript types and about typeof operator.


Based on the ES Specs the types are defined as -

β€œAn ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. ”

So it pretty much explains itself that types have some kind of values that can be manipulated by us using javascript.

Let’s talk about the primitive types that we have -

Undefined

It has only one value called undefined. Any variable which is not assigned a value has the value undefined.

Null

The Null type has exactly one value, called null. It represents an intentional absence of object value.

Boolean

This refers to two specific values - true & false.

String

The string type is generally used to represent the textual data. This is just the double quotes or single quotes string literal.

Number

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(2^53 βˆ’ 1) and 2^53 βˆ’ 1). Also, it has three other values namely: +Infinity, -Infinity, and NaN ("Not a Number").

Object

An Object is a collection of properties. Each property is either a data property or an accessor property.

Symbol

The Symbol type is the set of all non-String values that may be used as the key of an Object property. It is a unique and immutable primitive value.

Note - Functions and arrays are treated as sub-type of Object type.

Note - BigInt is now recognized as primitive type which is supported by most of the browsers.

typeof(23423432423423423424234n) // 'bigint'
Enter fullscreen mode Exit fullscreen mode

Read more about BigInt - here


typeof

We can use typeof operator to check the type of the value stored in a variable.

Let’s see some examples -

var a;

typeof(a); // 'undefined'

a = 5;
typeof(a); // 'number'

a = "Apple";
typeof(a); // 'string'

a = true;
typeof(a); // 'boolean'

a = {};
typeof(a); // 'object'

Enter fullscreen mode Exit fullscreen mode

Here, one thing to note is that it returns the type in string.

So if you have a piece of code like this -

if(typeof(a) === Number){
 ...
}
Enter fullscreen mode Exit fullscreen mode

It’s not gonna work as you intend to. The correct way to do this is by using number as a string.

if(typeof(a) === 'number'){
 ...
}
Enter fullscreen mode Exit fullscreen mode

Let’s have a look at some tricky ones -

typeof(doesntExist); // 'undefined'

var v = null;
typeof(v); // 'object'

v = function(){};
typeof(v); // 'function'

v = [1,2,3]
typeof(v); // 'object'

Enter fullscreen mode Exit fullscreen mode

Here we can see that the type of a variable which is not declared yet or doesn’t exist is β€˜undefined’.

Unlike typeof(undefined) which returns 'undefined', typeof(null) returns 'object' which is actually a historical bug. And now it can’t be fixed as a lot of legacy apps will break because of it.

Though function is not a type but it’s typeof() returns 'function' but that’s not the case with an array as typeof([]) returns β€˜object’.


I'll be posting more of my notes on JS topics, So stay tuned :)

Say Hi πŸ‘‹πŸ‘‹πŸ‘‹

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’ β€’ Edited

You've omitted BigInt

typeof 101010092092402940258250858205454867847n   // bigint
Enter fullscreen mode Exit fullscreen mode

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay