Skip to content

JavaScript Data Types

In computer languages, data types are used to classify a certain type of data. JavaScript is an object-oriented programming language, which means that it is based on the concept of objects and their relationships with one another. It is also a dynamically-typed language, which means that the data type of a variable is determined at runtime rather than at compile-time.

This is significant because the data type you pick will influence the values you can assign to it and the actions you can take with it. To put it another way, ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ฟ๐˜€๐˜๐—ฎ๐—ป๐—ฑ๐—ถ๐—ป๐—ด ๐˜๐—ต๐—ฒ ๐—ฑ๐—ฎ๐˜๐—ฎ ๐˜๐˜†๐—ฝ๐—ฒ ๐—ผ๐—ณ ๐—ฎ๐—ป๐˜† ๐˜ƒ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ ๐—ถ๐˜€ ๐—ฐ๐—ฟ๐—ถ๐˜๐—ถ๐—ฐ๐—ฎ๐—น ๐˜๐—ผ ๐—ฝ๐—ฒ๐—ฟ๐—ณ๐—ผ๐—ฟ๐—บ๐—ถ๐—ป๐—ด ๐—ผ๐—ฝ๐—ฒ๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐˜ƒ๐—ฎ๐—ฟ๐—ถ๐—ฎ๐—ฏ๐—น๐—ฒ๐˜€ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜.

Image source

Below are some tricky questions which generally asked during interview specific to JS data type:

๐—ฎ.) ๐—ช๐—ต๐—ฎ๐˜ ๐—ฎ๐—ฟ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ณ๐—ฎ๐—น๐˜€๐˜† ๐—ฎ๐—ป๐—ฑ ๐˜๐—ฟ๐˜‚๐˜๐—ต๐˜† ๐˜ƒ๐—ฎ๐—น๐˜‚๐—ฒ๐˜€ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜?

There are 6 values that are considered falsy in JavaScript:

  • The keyword false
  • The primitive value undefined
  • The primitive value null
  • The empty string (”, “”)
  • The global property NaN
  • A number or BigInt representing 0 (0, -0, 0.0, -0.0, 0n)

Every other value is considered truthy. It’s important to remember that this applies to all JavaScript values, even ones that might seem falsy, such as empty arrays ([]) or empty objects ({}).

๐—ฏ.) ๐—˜๐˜…๐—ฝ๐—น๐—ฎ๐—ถ๐—ป ๐—ฝ๐—ฎ๐˜€๐˜€๐—ฒ๐—ฑ ๐—ฏ๐˜† ๐˜ƒ๐—ฎ๐—น๐˜‚๐—ฒ ๐—ฎ๐—ป๐—ฑ ๐—ฝ๐—ฎ๐˜€๐˜€๐—ฒ๐—ฑ ๐—ฏ๐˜† ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ข๐—ฅ ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐˜๐—ต๐—ฒ ๐—ฑ๐—ถ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ฏ๐—ฒ๐˜๐˜„๐—ฒ๐—ฒ๐—ป ๐˜€๐˜๐—ผ๐—ฟ๐—ฒ๐—ฑ ๐—ฏ๐˜† ๐˜ƒ๐—ฎ๐—น๐˜‚๐—ฒ(๐—ฝ๐—ฟ๐—ถ๐—บ๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ) ๐—ฎ๐—ป๐—ฑ ๐˜€๐˜๐—ผ๐—ฟ๐—ฒ๐—ฑ ๐—ฏ๐˜† ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ (๐—ป๐—ผ๐—ป-๐—ฝ๐—ฟ๐—ถ๐—บ๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ)?

This can be tricky question to judge how much you are aware about value and reference type. Before that let’s first understand what is pass by value and pass by reference if someone is asking?

What is actually Pass by Value?
Pass by value in JavaScript means that a copy of the actual parameterโ€™s value is made in memory i.e., a new memory allocation is done, and all the changes are made in that new value (i.e., copied value). The original value and the copied value are independent of each other as they both have a different space in memory i.e., on changing the value inside the function, the variable outside the function is not affected.

What is Pass by Reference?
Pass by Reference in JavaScript does not create a new space in the memory, instead, we pass the reference/address of the actual parameter, which means the function can access the original value of the variable. Thus, if we change the value of the variable inside the function, then the original value also gets changed.

Is JS pass by value or pass by reference?
Everything is passed by value, but if your value is an object, then the new scope gets a copy of the reference. If you modify the object by dereferencing, your modifications to the object persist outside of your scope. In JavaScript, there is no such thing as โ€œpass by referenceโ€ for any variable. All variables and arguments have a value given to them, however the value of an objectโ€™s variable is a reference. As a result, if you supply an object and alter its members inside the method, those changes will remain outside of the function. This makes it appear as if itโ€™s a pass-by-reference system. Primitive values like integer, string, and boolean are provided by value, but objects and arrays, as previously stated, are passed by reference.

๐—ฐ.) ๐—ป๐˜‚๐—น๐—น ๐—ฎ๐—ป๐—ฑ ๐˜‚๐—ป๐—ฑ๐—ฒ๐—ณ๐—ถ๐—ป๐—ฒ๐—ฑ ๐—บ๐—ฒ๐—ฎ๐—ป ๐˜๐—ต๐—ฒ ๐˜€๐—ฎ๐—บ๐—ฒ ๐˜๐—ต๐—ถ๐—ป๐—ด?

In simple words, undefined means a variable has been declared but has not yet been assigned a value. undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of undefined.

If a variable is null then it means the variable has no value and that it was explicitly set to have no value by the programmer. A variable will never be null unless somewhere in the code a programmer set a variable to null.

The data type of undefined is undefined whereas that of null is object.ย Also, when used in arithmetic operations,ย undefinedย will result inย NaNย (not a number), whereasย nullย will be converted toย 0ย behind the screens.

We can say that by setting a variable to undefined you are conveying the message that the variable no longer contains any useful information, while if the value is null then you are specifically saying the result of some action has no value.

๐—ฑ.) ๐—ช๐—ต๐˜† ๐—ถ๐˜€ ๐—ป๐˜‚๐—น๐—น ๐—ฎ๐—ป ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜?

In JavaScript, typeof null is ‘object’, which incorrectly suggests that null is an object (it isnโ€™t, itโ€™s a primitive value). This is a bug and one that unfortunately canโ€™t be fixed, because it would break existing codebase. null was a representation of the null pointer. However, there were no pointers in JavaScript like C. So null simply meant nothing or void and was represented by all 0โ€™s. Hence all its 32 bits were 0โ€™s. So whenever the JavaScript interpreter reads null, it considers the first 3 bits as type โ€œobjectโ€. That is why typeof null returns โ€œobjectโ€.
This may seem like a very obvious bug, but donโ€™t forget that there was very little time to finish the first version of JavaScript. ๐Ÿ˜‰

๐—ฒ.) ๐—›๐—ผ๐˜„ ๐˜„๐—ผ๐˜‚๐—น๐—ฑ ๐˜†๐—ผ๐˜‚ ๐—ฐ๐—ผ๐—บ๐—ฝ๐—ฎ๐—ฟ๐—ฒ ๐˜๐˜„๐—ผ ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜๐˜€ ๐—ถ๐—ป ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜?

๐—ณ.) ๐—œ๐—ณ [] ๐—ถ๐˜€ ๐˜๐—ฟ๐˜‚๐—ฒ, ๐˜๐—ต๐—ฒ๐—ป []==๐˜๐—ฟ๐˜‚๐—ฒ ๐˜€๐—ต๐—ผ๐˜‚๐—น๐—ฑ ๐—ฎ๐—น๐˜€๐—ผ ๐—ฏ๐—ฒ ๐˜๐—ฟ๐˜‚๐—ฒ. ๐—ฟ๐—ถ๐—ด๐—ต๐˜?

๐—ด.) ๐—ช๐—ต๐—ฎ๐˜ ๐˜„๐—ถ๐—น๐—น ๐—ฏ๐—ฒ ๐˜๐—ต๐—ฒ ๐Ÿต๐Ÿฎ..๐˜๐—ผ๐—ฆ๐˜๐—ฟ๐—ถ๐—ป๐—ด()?

๐—ต.) ๐—ช๐—ต๐—ฎ๐˜ ๐˜„๐—ถ๐—น๐—น ๐—ฏ๐—ฒ ๐˜๐—ต๐—ฒ ๐˜๐˜†๐—ฝ๐—ฒ๐—ผ๐—ณ(๐—ก๐—ฎ๐—ก)?

๐—ถ.) ๐——๐—ผ๐—ฒ๐˜€ ๐—๐—ฎ๐˜ƒ๐—ฎ๐—ฆ๐—ฐ๐—ฟ๐—ถ๐—ฝ๐˜ ๐˜€๐˜‚๐—ฝ๐—ฝ๐—ผ๐—ฟ๐˜ ๐—ฎ๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ฐ ๐˜๐˜†๐—ฝ๐—ฒ ๐—ฐ๐—ผ๐—ป๐˜ƒ๐—ฒ๐—ฟ๐˜€๐—ถ๐—ผ๐—ป?

๐—ท.) ๐—ช๐—ต๐—ฒ๐—ฟ๐—ฒ ๐—ฎ๐—ฟ๐—ฒ ๐—ฟ๐—ฒ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ๐˜€ (๐˜„๐—ต๐—ถ๐—ฐ๐—ต ๐—ฝ๐—ผ๐—ถ๐—ป๐˜ ๐˜๐—ผ ๐—ผ๐—ฏ๐—ท๐—ฒ๐—ฐ๐˜๐˜€ ๐—ฎ๐—ป๐—ฑ ๐—ณ๐˜‚๐—ป๐—ฐ๐˜๐—ถ๐—ผ๐—ป๐˜€) ๐˜€๐˜๐—ผ๐—ฟ๐—ฒ๐—ฑ? ๐—ฆ๐˜๐—ฎ๐—ฐ๐—ธ ๐—ผ๐—ฟ ๐—›๐—ฒ๐—ฎ๐—ฝ?

๐—ธ.) ๐——๐—ถ๐—ณ๐—ณ๐—ฒ๐—ฟ๐—ฒ๐—ป๐—ฐ๐—ฒ ๐—ฏ๐—ฒ๐˜๐˜„๐—ฒ๐—ฒ๐—ป === ๐˜ƒ๐˜€ == ?

There are many more questionsโ€ฆ. where interviewer can ask indirectly about data type. Sometimes people are confused and telling Integer as a data type instead of Number during answer.

I am in process of collating more interviews questions (topic specific) which are basically asked during frontend developer interview and will publish soon on my LinkedIn post.

๐—ฃ๐—น๐—ฒ๐—ฎ๐˜€๐—ฒ ๐—ณ๐—ฒ๐—ฒ๐—น ๐—ณ๐—ฟ๐—ฒ๐—ฒ ๐˜๐—ผ ๐—ฎ๐—ฑ๐—ฑ ๐—ฎ๐—ป๐˜† ๐—พ๐˜‚๐—ฒ๐˜€๐˜๐—ถ๐—ผ๐—ป ๐˜๐—ต๐—ฎ๐˜ ๐˜†๐—ผ๐˜‚ ๐—ต๐—ฎ๐˜ƒ๐—ฒ ๐—ฒ๐—ป๐—ฐ๐—ผ๐˜‚๐—ป๐˜๐—ฒ๐—ฟ๐—ฒ๐—ฑ ๐—ฑ๐˜‚๐—ฟ๐—ถ๐—ป๐—ด ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„ ๐—ถ๐—ป ๐—ฐ๐—ผ๐—บ๐—บ๐—ฒ๐—ป๐˜ ๐˜€๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป, ๐˜๐—ต๐—ถ๐˜€ ๐˜„๐—ถ๐—น๐—น ๐—ต๐—ฒ๐—น๐—ฝ ๐—ผ๐˜๐—ต๐—ฒ๐—ฟ ๐—ณ๐—ฟ๐—ผ๐—ป๐˜๐—ฒ๐—ป๐—ฑ ๐—ฑ๐—ฒ๐˜ƒ๐—ฒ๐—น๐—ผ๐—ฝ๐—ฒ๐—ฟ ๐—ท๐—ผ๐—ฏ ๐—ฎ๐˜€๐—ฝ๐—ถ๐—ฟ๐—ฎ๐—ป๐˜๐˜€. 

Leave a Reply

Your email address will not be published. Required fields are marked *