The basics of syntax

INPUT · Slides

Text and numbers are different things

01 / 05

A number is not wrapped in "

When you want a number, you write it as it is — no " around it.

Leave it bare and JS understands "this is a number".

console.log(42);

Result

42

02 / 05

Wrapped or bare, they are not the same

"42" and 42 look identical on the screen, but inside JS they are two different things.

  • "42" … text (we call it a string)
  • 42 … a number

Same look, different handling. This is one of the first places people trip up.

console.log("42");console.log(42);

Result

42
42

03 / 05

You see the difference when you do maths

A number can be worked out. A string cannot.

Wrap it in " and it stops being a sum — it becomes just some characters.

console.log(3 + 5);console.log("3 + 5");

Result

8
3 + 5

04 / 05

A mistake people make

A phone number or a postcode looks like a number, but you normally hold it as a string.

As a number the leading 0 disappears, and you are never going to add it to anything anyway.

The question is not "does it look like digits" but "am I going to do maths with it".

console.log(0123);console.log("0123");

Result

83
0123

05 / 05

How to decide

When you are not sure, think of it this way.

  • Words for a person to read → a string (wrapped in ")
  • A number you will do maths with → a number (left bare)

These two come up again and again from here on. When you hunt down an error, the first thing you will check is "is this a string or a number".

Let us get the difference into your fingers.