- Numbers and arithmetic operations
- Strings and interpolation
- Booleans, comparison and logic operations
In JavaScript, a “type” tells you what kind of data a value is.
Javascript has a few basic types, and in this section we'll explore some of them.
Numbers and arithmetic operations
The first type we're gonna introduce is
number.Numbers in JavaScript can be integers (like
5) or decimals (like 3.14).You can do arithmetic with them: addition, subtraction, multiplication, and division.
Here’s a basic example:
const a = 10 const b = 5 const sum = a + b const difference = a - b const product = a * b const quotient = a / b console.log(sum) console.log(difference) console.log(product) console.log(quotient)
This will print:
15 5 50 2
You can also use parentheses
() to control the order of operations:const result = (2 + 3) * 4 console.log(result)
This prints:
20
Without the parentheses, it would be
2 + 3 * 4, which is:const result = 2 + 3 * 4 console.log(result)
That would print:
14
Because in regular math, multiplication happens before addition.
Strings and interpolation
The second JavaScript type we're gonna introduce is
string.Strings are pieces of text. You can use single quotes
'...' or double quotes "..." to create them.const greeting = "hello" const name = 'Bob' console.log(greeting) console.log(name)
This prints:
hello Bob
To combine strings, you can use the
+ operator:const greeting = "hello" const space = " " const name = "Bob" const fullGreeting = greeting + space + name console.log(fullGreeting)
This will print:
hello Bob
But there is a nicer way to combine strings called string interpolation. You use backticks to declare the string
`...` and write variables using ${...} inside the string:const greeting = "hello" const name = "Bob" const fullGreeting = `${greeting} ${name}` console.log(fullGreeting)
This also prints:
hello Bob
You can include any expression inside
${...}:const age = 30 console.log(`Next year, I will be ${age + 1} years old.`)
This prints:
Next year, I will be 31 years old.
Interpolation is very common in modern JavaScript.
Booleans, comparison and logic operations
The third type we're gonna introduce is
boolean. It is named after the mathematician George Boole, who invented boolean logic.Booleans are simple: only two possible values,
true and false.You can store them in variables:
const theSkyIsBlue = true const thisCourseIsBad = false console.log(theSkyIsBlue) console.log(thisCourseIsBad)
This prints:
true false
You can combine booleans using logic operators:
&&means “and”, and it will returntrueonly if both values aretrue, otherwise it will returnfalse||means “or”, and it will returntrueif at least one of the values istrue, otherwise (if they're both false) it will returnfalse!means “not”, it's applied before a boolean, and it will flip it: if the boolean it'strueit will returnfalse, and vice versa.
Examples:
const isSunny = true const isWarm = true console.log(isSunny && isWarm) // true console.log(isSunny || isWarm) // true console.log(!isSunny) // false
You can compare values in JavaScript using operators like
>, <, ===, and !==. The result of these comparisons is always a boolean.const first = 10 const second = 5 const firstIsGreater = (a > b) const secondIsGreater = (a < b) const theyAreEqual = (a === b) const theyAreDifferent = (a !== b) console.log(firstIsGreater) // true console.log(secondIsGreater) // false console.log(theyAreEqual) // false console.log(theyAreDifferent) // true
Javascript also has
>= to mean "bigger or equal" and <= to mean "smaller or equal".Booleans, comparison and logical operators are often combined in programs to declare complex conditions, like to ensure "the email has arrived AND it contains the image I need OR the length of the email is longer than 10000 characters". You will find later that these are essential building blocks to construct the logic of the program.