Langsung ke konten

Pelajaran 1 dari 109 menit

Values and variables

Every program moves values around. Meet JavaScript's handful of value types and the two keywords you need to name them.

Pelajaran ini hanya tersedia dalam bahasa Inggris. Antarmuka halaman tetap dalam bahasa Anda.

Dalam pelajaran ini

  • Name JavaScript's primitive types and recognise objects
  • Choose between const and let
  • Compare values safely with ===

Values come in a few kinds#

A program is a set of instructions for transforming values: a price, a user's name, whether a box is ticked. JavaScript has seven primitive types and one structured type, the object.

TypeExampleUsed for
number42, 3.14Counts, prices, measurements
string"hello"Text
booleantrue, falseYes/no decisions
undefinedundefined"No value was given"
nullnull"Deliberately empty"
bigint10nWhole numbers beyond about 9 quadrillion
symbolSymbol("id")Unique keys (rare in everyday code)

Everything else (arrays, functions, dates) is an object. You can always ask what you are holding with typeof.

typeof.ts
console.log(typeof 42); // "number"console.log(typeof "hi"); // "string"console.log(typeof true); // "boolean"console.log(typeof null); // "object"1console.log(typeof [1, 2]); // "object"

typeof null returning "object" is a bug from 1995 that can never be fixed without breaking the web. Check for null with value === null.

Naming values: const and let#

A variable is a name that points at a value. Modern JavaScript gives you two ways to create one.

variables.ts
const taxRate = 0.11;1let total = 100;2total = total + total * taxRate;console.log(total); // 111

const means the name can never point at another value. Reach for it first.

let allows reassignment. Use it only when the value genuinely changes, like a running total.

You will also see var in older code. It behaves differently around scope (the next lesson explains scope) and there is no reason to use it in new code.

A habit that pays off

Write const by default. If the editor complains that you reassign it, decide whether the change is really needed before switching to let.

const does not freeze objects#

const protects the name, not the contents. An object assigned to a const can still change inside.

const-object.ts
const cart = { items: 0 };cart.items = 3; // allowed: the object changed, the name did not1console.log(cart.items); // 3

Reassigning the name itself (cart = {}) is what const forbids.

Comparing values#

Use === (strict equality). It compares without converting types. The older == converts first, which produces surprises.

=== strict

  • 1 === 1 is true
  • 1 === "1" is false
  • What you almost always want

== loose

  • 1 == "1" is true
  • 0 == "" is true
  • Avoid it; the conversion rules are hard to remember

Peringatan

Numbers are floating point: 0.1 + 0.2 === 0.3 is false because the sum is 0.30000000000000004. For money, count in the smallest unit (cents, sen) as whole numbers.

Template strings#

Backticks let you build strings with values inside them.

template.ts
const name = "Ayu";const items = 3;console.log(`${name} has ${items} items in the cart.`);

LatihanPrice with tax

Store a price of 250 and a tax rate of 11% in two constants. Print a sentence like Total: 277.5, using a template string.

Tampilkan solusi
solution.ts
const price = 250;const taxRate = 0.11;console.log(`Total: ${price + price * taxRate}`);

Recap#

  • Seven primitive types plus objects; check with typeof, but test null with === null.
  • const by default, let when the value must change, never var.
  • Compare with ===.