Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript5 tutorials
  • Node.js Introduction2 tutorials
  • JavascriptJavascript · Beginner · Mid
  • JavascriptJavascript · Beginner · Short
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 3

Values and Data Types

In this chapter, you will learn what a **value** is and what a **data type** is. A *value* is a single piece of data, like the number `7` or the word `"hello"`. A *data type* is the kind of value it is. JavaScript uses the type to decide what it can do with a value — for example, you can do math wit
Level: BeginnerLength: Mid2 min read

Read at another depth

Values and Data Types

In this chapter, you will learn what a value is and what a data type is. A value is a single piece of data, like the number 7 or the word "hello". A data type is the kind of value it is. JavaScript uses the type to decide what it can do with a value — for example, you can do math with numbers, but not with words.

Why Types Matter

Every value in JavaScript has exactly one type. When you combine values, JavaScript follows rules based on those types. If you know the type, you can predict what your code will do. If you don't, you get surprises — like "2" + 2 turning into the string "22" instead of the number 4.

The Core Types

Strings

A string is text wrapped in quotes. It can hold letters, numbers, spaces — anything you can type.

Code example in javascript
const greeting = "Hello, world!";
const name = 'Ada';

You can use double quotes or single quotes. Backticks (` ``) also make strings and let you insert values inside them:

Code example in javascript
const name = 'Ada';
const message = `Hi, ${name}!`; // "Hi, Ada!"

Numbers

A number is a value for math. JavaScript has one number type for both whole numbers and decimals.

Code example in javascript
const age = 30;      // whole number
const price = 9.99;  // decimal

Booleans

A boolean is a value that is either true or false. Booleans power decisions, like "is the user logged in?"

Code example in javascript
const isLoggedIn = true;
const hasPermission = false;

null and undefined

These two types both mean "no value," but in different ways:

  • null means "intentionally empty." A programmer set it on purpose.
  • undefined means "not set yet." If you create a variable and never give it a value, it is undefined.
Code example in javascript
let nextStep;          // undefined — nothing assigned yet
nextStep = null;       // null — deliberately empty for now

Checking a Type with typeof

The typeof operator tells you the type of any value as a string:

Code example in javascript
console.log(typeof "hi");      // "string"
console.log(typeof 42);        // "number"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null);      // "object" (a famous old bug!)

Putting Types Together

Try this in your browser console (press F12 and click the Console tab):

Code example in javascript
console.log(2 + 2);        // 4 — number plus number
console.log("2" + 2);      // "22" — string plus number makes a string!
console.log("Hi " + 5);    // "Hi5"
console.log(10 > 3);       // true — comparison gives a boolean

Notice "2" + 2: when you use + with a string, JavaScript converts the other value to a string and glues them together. With any other math operator, it tries to convert to a number instead:

Code example in javascript
console.log("6" - 2);  // 4
console.log("6" * 2);  // 12

The safe habit: keep your types consistent, and use typeof when you are unsure.

Exercise

What does typeof "42" return, and why?

Show answer

It returns "string". The quotes make "42" a string of characters, not the number 42. Remove the quotes and typeof 42 returns "number".

Exercise

What is the difference between null and undefined?

Show answer

undefined means a value has not been set yet. null means a value was deliberately set to "nothing." Both mean "no value," but null is a choice a programmer makes.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Values and Data Types

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that