Number
Numbers can be used for arithmetic like 1 + 1.
String
Strings are text and can be concatenated like "Hello " + "World".
Operator
A symbol that tells JavaScript to perform an operation.
Data Types
Every piece of data in JavaScript has a type.
Example
Code
console.log(typeof "1")
console.log(typeof 1)Notes
- Types tell JavaScript how data can be used.
"1"is text.1is a number.
String Concatenation
Adding strings joins them together.
Example
"1" + "1"
"Hello " + "World"
Code
console.log("1" + "1")
console.log("Hello " + "World")Notes
+adds numbers.+concatenates strings.- The type changes what the operator does.
Demo: Bank Deposit Bug
The bug happens because the balance on the page is text, not a number.
Example
Goal: The balance starts at 1000. After depositing 100, students probably expect 1100.
How the HTML connects to the JavaScript
id="balance" is the balance number JavaScript reads and changes.
id="deposit" is the button JavaScript listens to for a click.
1000 + 100 = 1100"1000" + 100 = "1000100"Code
<p id="balance">1000</p>
<button id="deposit">Deposit 100</button>Buggy JavaScriptvar balanceElement = document.getElementById("balance")
var currentBalance = balanceElement.innerHTML
// currentBalance is text: "1000"
balanceElement.innerHTML = currentBalance + 100Fixed codevar balanceElement = document.getElementById("balance")
var currentBalance = Number(balanceElement.innerHTML)
// currentBalance is now a number: 1000
balanceElement.innerHTML = currentBalance + 100Notes
- Step 1:
document.getElementById("balance")finds the balance element. - Step 2:
balanceElement.innerHTMLreads what is displayed, but it reads it as text:"1000". - Step 3:
"1000" + 100joins text, so the result is"1000100". - Fix: use
Number(...)before adding, so JavaScript does real maths.
Casting
Casting means changing one data type into another type.
Example
Number("1337") becomes 1337.
String(133) becomes "133".
Code
var numberValue = Number("1337")
var stringValue = String(133)
console.log(numberValue + 7)
console.log(stringValue + 7)Notes
Number()converts to a number.String()converts to text.- Explicit casting reduces confusing bugs.
NaN
NaN means Not a Number.
Example
Number("abc")
NaN + 1
Code
console.log(Number("abc"))
console.log(NaN + 1)
console.log(NaN * 100)Notes
- If a string does not represent a number,
Number()returnsNaN. - Operations with
NaNusually produceNaN. - It is JavaScript saying, "this is not a usable number."
Arithmetic Operators
Arithmetic operators take numerical values and produce a number.
Example
Code
console.log(10 + 2)
console.log(10 - 2)
console.log(10 * 2)
console.log(10 / 2)
console.log(10 % 3)
console.log(2 ** 3)Notes
- Operands are the values the operator works on.
++increases by 1.--decreases by 1.
Assignment Operators
Assignment operators update variables.
Example
var a = 3
a += 2 makes a become 5.
a *= 4 makes a become 20.
Code
var a = 3
a += 2
a *= 4
a -= 5
a /= 5
a %= 2Notes
=assigns a value.+=,-=,*=,/=,%=, and**=update existing values.- Strings also support
+=for concatenation.
Mixing Types
Guess the result first, then reveal the answer.
Example
Question 1: What is "1" + 1?
"11". Because one operand is a string, + performs string concatenation.Question 2: What is "1" - 1?
0. Strings do not have a subtraction operation, so JavaScript casts "1" to the number 1.Question 3: What is "1" - "1"?
0. Both strings are cast to numbers before subtraction.Code
console.log("1" + 1)
console.log("1" - 1)
console.log("1" - "1")Notes
- Pause and guess before revealing each answer.
- Any
+operation with a string operand performs concatenation. - Other arithmetic operators try to cast operands to numbers.
- This is why explicit casting is safer.
Mini Playground: Operator Lab
Try values and operators to see the result and type.
Example
Code
var result = left + right
console.log(result)
console.log(typeof result)Notes
- Inputs start as strings.
- Use
Number()when you want numeric arithmetic. - Observe how
+behaves differently from-.
Slide Coverage Map
Number() are covered in sections 3 and 4.NaN and converting numbers to strings are covered in sections 4 and 5.