TechLead
Lesson 3 of 16

Data Types

Work with strings, numbers, booleans, null, and undefined.

Quick Summary

JavaScript is dynamically typed, meaning a variable's type comes from the value it holds at runtime. The runtime tracks types as values flow through your program, choosing behavior based on the current value and sometimes applying automatic type coercion. Numbers are stored as 64-bit floating point (IEEE 754), strings are immutable sequences of characters, and booleans represent true/false logic. `null` is an intentional empty value, while `undefined` means 'not assigned yet.' Understanding these types helps you avoid surprises with comparisons and conversions.

Primitive Data Types

JavaScript has 7 primitive data types. Here are the most common:

String

Text data wrapped in quotes:

const single = 'Hello';
const double = "World";
const template = `Hello, ${name}!`; // Template literal

Strings are immutable — you can't change individual characters:

let word = "hello";
word[0] = "H"; // Does nothing
word = "Hello"; // Creates new string

Number

JavaScript has one number type for both integers and decimals:

const integer = 42;
const decimal = 3.14;
const negative = -17;
const scientific = 2.5e6; // 2,500,000

Special number values:

const infinite = Infinity;
const notNumber = NaN; // "Not a Number"

Boolean

Only two values: true or false:

const isActive = true;
const hasError = false;
const comparison = 5 > 3; // true

null and undefined

let noValue = null;      // Intentionally empty
let notSet;              // undefined (not assigned)
let explicit = undefined; // Also undefined

typeof Operator

Check a value's type:

typeof "hello"    // "string"
typeof 42         // "number"
typeof true       // "boolean"
typeof undefined  // "undefined"
typeof null       // "object" (historical bug)

Type Coercion

JavaScript automatically converts types in certain situations:

"5" + 3    // "53" (number becomes string)
"5" - 3    // 2 (string becomes number)
!!"hello"  // true (truthy to boolean)

Try It Yourself

Here's a practical example you can try. Copy this code and run it in your browser's console (press F12 to open developer tools) or in the Code Playground.

// String - text data
const greeting = "Hello, World!";
const name = 'Alice';
const message = `Welcome, ${name}!`; // Template literal

// Number - integers and decimals
const age = 25;
const price = 19.99;
const temperature = -5;

// Boolean - true or false
const isLoggedIn = true;
const hasPermission = false;

// null - intentional "no value"
const emptyResult = null;

// undefined - value not yet assigned
let futureValue;
console.log(futureValue); // undefined

// Check types with typeof
console.log(typeof greeting);  // "string"
console.log(typeof age);       // "number"
console.log(typeof isLoggedIn); // "boolean"

Key Takeaways

  • Work with strings, numbers, booleans, null, and undefined.
  • Practice with real code examples to solidify your understanding
  • This concept builds the foundation for more advanced topics

Related Learning Resources

Continue your programming journey with these related tutorials: