OnlineCBT Logo OnlineCBT
JavaScript Tutorial
JAVASCRIPT · LESSON 4

Data Types and Type Conversion

By Ratan Updated: 16 Oct 2026

Explore primitive types (string, number, boolean, null, undefined, symbol, bigint) and coercion.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Know the value before converting it

JavaScript primitives include string, number, bigint, boolean, undefined, symbol and null. Objects, including arrays and functions, have reference identity. typeof is useful, but its historical result for null is 'object'; use a direct null check when that distinction matters.

console.log(typeof '42'); // string
console.log(typeof 42);   // number
console.log(typeof null); // object (historical behavior)
console.log(Array.isArray([1, 2])); // true

const entered = '42';
const score = Number(entered);
console.log(score + 8); // 50
console.log(entered + 8); // 428

Convert form input explicitly

Ordinary input.value is a string. Number converts the complete numeric representation, while parseInt reads an integer prefix. Number('12px') is NaN; parseInt('12px', 10) is 12. Choose the behavior you need rather than using prefix parsing as full validation.

function readScore(text) {
  if (text.trim() === '') return null;
  const value = Number(text);
  return Number.isFinite(value) && value >= 0 && value <= 100
    ? value : null;
}
console.log(readScore('82')); // 82
console.log(readScore(''));   // null
console.log(readScore('abc')); // null

The empty-string check matters because Number('') is 0. Decide separately whether fractional marks are allowed; use Number.isInteger if the requirement is whole numbers only.

Truthiness and missing values

false, zero, the empty string, null, undefined and NaN are common falsy values. Empty arrays and empty objects are truthy. Boolean('false') is true because the string is non-empty; it does not parse a textual boolean. Map accepted text values explicitly.

Numeric limits

Number uses floating-point representation. Decimal arithmetic can have rounding effects, and integers outside the safe range may lose precision. BigInt supports arbitrary-sized integers but cannot be mixed directly with Number in ordinary arithmetic. JSON also needs an explicit strategy for BigInt.

Practice

  1. Compare Number('08') and parseInt('08',10).
  2. Test score input containing whitespace, Infinity and -1.
  3. Explain why an empty object passes an if condition.
  4. Use Number.isNaN rather than comparing a value to NaN with equality.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat