Categories
JavaScript Interview Questions

JavaScript Interview Questions — Data Types

Spread the love

To get a job as a front end developer, we need to nail the coding interview.

In this article, we’ll look at how some data type questions, including some trick questions that are often asked in interviews.

What are the falsy values in JavaScript?

In JavaScript, falsy values are the following

  • ''
  • 0
  • null
  • undefined
  • false
  • NaN

They become false when they’re converted to a boolean value.

How to check if a value is falsy?

Use the !! operator or Boolean function to check if they’re falsy.

If a value is falsy, then both will return false.

For example:

!!0

will return false.

Boolean(0)

will also return false.

What are truthy values?

Truthy values are any other than those falsy values listed above.

What are Wrapper Objects?

Wrapper objects are objects that are created from constructors that return primitive values but have the type 'object' if it’s a number or a string. Symbols and BigInt have their own types.

In JavaScript, there’s the Number, String constructors, and Symbol, and BigInt factory functions.

Primitive values are temporarily converted to wrapper objects to call methods.

For example, we can write:

'foo'.toUpperCase()

Then we get 'FOO'. It works by converting the 'foo' literal to an object and then call the toUpperCase() method on it.

We can also create wrapper objects for numbers and BigInt as follows:

new Number(1)
BigInt(1)

Using the Number or String constructor directly isn’t too useful, so we should avoid it. They give us the type of object but has the same content as their primitive counterparts.

If we did create Number and String wrapper objects with the new operator as follows:

const numObj = new Number(1);
const strObj = new String('foo');

We can return the primitive value version of those wrapper objects by using the valueOf method as follows:

numObj.valueOf();
strObj.valueOf();

What is the difference between Implicit and Explicit Coercion?

Implicit coercion means that the JavaScript interpreter converts a piece of data from one type to another without explicitly calling a method.

For example, the following expression are implicitly converted to a string:

console.log(1 + '2');

Then we get:

'12'

returned because 1 is converted to '1' implicitly.

Another example would be:

true + false

We get 1 returned because true is converted to 1 and false is converted to 0 and they’re added together.

The following example:

3 * '2'

returns 6 because '2' is converted to 2 and then they’re multiplied together.

The full list of JavaScript coercion rules are here.

Explicit conversion is done by calling a method or using an operator to convert a type.

We can see the coercion done in the code.

For example:

+'1' + +'2'

gets us 3 since we have '1' and '2' are both converted to numbers by the unary + operator.

We can also do the same things with functions. For example, we can use the parseInt function instead of using the unary + operator.

We can rewrite the expression above to:

parseInt('1') + parseInt('2')

Then we get the same result.

What Does 0 || 1 Return?

Since 0 is falsy, it’ll trigger the second operand to run, which gives us 1. Therefore, it should return 1.

What Does 0 && 1 Return?

Since 0 is falsy, it’ll stop running since we have short-circuited evaluation. Therefore, we get 0 returned.

What Does false == ‘0’ Return?

false is falsy and '0' is converted to 0, which is falsy, so they are the same in terms of the ==’s comparison criteria. Therefore, we should get true returned.

What Does 4 < 5 < 6 Return?

In JavaScript, comparisons are done from left to right. So first 4 < 5 will be evaluated, which gives us true . Then true < 6 is evaluated, which is converted to 1 < 6 which is also true .

Therefore, we get true returned.

What Does 6 > 5 > 4 Return?

In JavaScript, comparisons are done from left to right. So 6 > 5 will be evaluated first. This gives us true . Then true > 4 is evaluated, which is converted to 1 > 4 , which is false .

Therefore, the expression returns false .

What Does typeof undefined == typeof null Return?

typeof undefined returns 'undefined' and typeof null returns 'object' .

Since 'undefined' does equal 'object' , the expression should be false .

What Does typeof typeof 10 Return?

JavaScript evaluates typeof typeof 10 as typeof (typeof 10) . Therefore, this returns typeof 'number' , which returns 'string' since 'number' is a string.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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