HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›Data Types

Data Types

string, number, boolean types; checking types with typeof and conversion with Number()/String().

Simple enough for a 5-year-old to understand

Basic types

Example code
1
const name = "Alex";
string — text.
Variablestring — text.
2
const age = 36;
number — number (integer or decimal).
Variablenumber — number (integer or decimal).
3
const student = true;
boolean — true/false.
Variableboolean — true/false.
4
console.log(typeof name, typeof age, typeof student);
string number boolean
Output / Inputstring number boolean
VariableOutput / Input
💡 Hint: There is no distinction for integers and decimals in JavaScript; they are all "number".

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.What does typeof 5 return?

About this topic

There are seven fundamental types in JavaScript, but in everyday code you work with four: text (string), number (number), logical value (boolean), and object (object). Arrays are actually also objects. You can ask for the type of a value using the typeof operator.

The most criticized aspect of JavaScript is that it performs type conversion automatically. When you add a number and a text, it doesn't throw an error; it converts the number to text and concatenates them. This flexibility sometimes makes your job easier, but most of the time it produces hard-to-notice bugs. That's why using strict equality (three equals) in comparisons is a critical habit.

What you will learn

  • learning how to check types with typeof
  • how automatic type coercion works
  • performing explicit conversion with Number, String, and parseInt
  • the difference between double equals and triple equals
  • what the NaN value means

When to use?

Type conversion is most needed when receiving data from a form: every value read from an input field is text, even if it looks like a number. You need to convert it with Number before doing calculations.

Always use strict equality (three equals) in comparisons. Double equals tries to coerce types and yields unexpected results like "0 equals empty text".

Common mistakes

Wrong

const a = "5";
console.log(a + 1);

The plus sign performs concatenation when one side is text; the result becomes "51" instead of 6. You don't get an error message, the code fails silently — which is why it is hard to notice.

Correct

const a = "5";
console.log(Number(a) + 1);

Wrong

if (0 == "") {
  console.log("equal");
}

Since double equals performs type coercion, this condition evaluates to TRUE, whereas zero and empty text are not the same thing. When you use triple equals, types are also compared and the result correctly evaluates to false.

Correct

if (0 === "") {
  console.log("equal");
}

Wrong

const n = Number("abc");
if (n === NaN) {
  console.log("not a number");
}

NaN is not equal to anything, not even itself; this comparison never evaluates to true. You need to check if a value is NaN using Number.isNaN.

Correct

const n = Number("abc");
if (Number.isNaN(n)) {
  console.log("not a number");
}

Frequently asked questions

why does typeof say object for an array?

Because in JavaScript, an array is not a separate type, but a special object. To figure out if a value is an array, you need to use Array.isArray. typeof null also returns object; this is a known inconsistency from the first version of the language that couldn't be fixed.

what is NaN?

Not-a-Number. It appears when a numeric operation cannot produce a meaningful result — for example, when you try to convert a piece of text into a number. NaN is contagious: the result of any calculation it enters also becomes NaN, so it's important to catch it early.

what is the difference between parseInt and Number?

Number tries to convert the entire text into a number; if it cannot, it returns NaN. parseInt starts from the beginning and reads up to the first non-numeric character, turning "12px" into 12. If you want a complete conversion use Number, if you want to extract the leading number use parseInt.

Summary

Because JavaScript automatically converts types, errors remain silent: text can be added to a number, double equals produces unexpected equalities. The way to protect yourself is clear — use triple equals in comparisons, and explicitly use Number or String for conversion.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code