JavaScript Course
JavaScript
/
Beginner

Data Types & Coercion

Definition

JavaScript has different types of values, broadly categorized into Primitives (like strings and numbers) and Objects. Type Coercion is JavaScript's automatic conversion of values from one data type to another.

Explain Like I'm New

Imagine trying to add the word 'Apple' and the number 5. JavaScript tries to be helpful by automatically turning the number 5 into a word, so it becomes 'Apple5'. This automatic conversion is called coercion.

Real World Example

When a user types their age into a form, it's often captured as a text string (e.g., '25'). When you try to add 5 years to it, JavaScript needs to know if it should combine them like text ('255') or do math (30).

Common Use Cases

  • •Validating user input
  • •Checking if a value is 'truthy' or 'falsy'
  • •Comparing variables for equality

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What are the primitive data types in JavaScript?
  • What is the difference between == and ===?

intermediate

  • What is type coercion?
  • What are 'truthy' and 'falsy' values?

advanced

  • Explain the difference between null and undefined.
  • How does JavaScript coerce objects to primitive values?

trick

  • What does [] == ![] evaluate to and why?
  • What is the result of typeof null?

Flash Cards

Question

What are the primitive data types?

Click to reveal answer
Answer

String, Number, BigInt, Boolean, Undefined, Null, and Symbol.

Question

What is the difference between == and ===?

Click to reveal answer
Answer

== (loose equality) compares values after attempting to convert them to the same type (coercion). === (strict equality) compares both value and type without converting.

Question

What is the result of typeof null?

Click to reveal answer
Answer

It returns 'object'. This is a known historical bug in JavaScript that was never fixed to maintain backward compatibility.