Strings, numbers and booleans

Hey ,

I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.

Please upgrade (use this link) access this content.

I'm super eager to help you learn more!

Strings, numbers and booleans

What is 1 + 1?

In JavaScript, the answer can be 2 or 11. It depends on how 1 + 1 is written.

To understand why the answer can be 2 or 11, you need to understand JavaScript basic types and how they work.

The seven data types

JavaScript has seven basic data types in total. These data types are also called primitives. They are:

  1. String
  2. Number
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol
  7. BigInt

String

A String represents text data in JavaScript. You create strings by enclosing them in quotation marks:

'JavaScript is easier than I thought!'

If you log this string into Chrome’s console, you’ll see black text. This is how you can quickly tell if something is a string in the console.

console.log('JavaScript is easier than I thought!')
Text that appears black in a JavaScript console represents a String
Text that appears black in a JavaScript console represents a String

You can use singular quote marks ('') or double quote marks ("") to create a string. What you use comes down to your preference.

Some people prefer using the single marks, others prefer double marks. What’s important is to be consistent. If you use single marks, make sure you use single marks all the way.

In this course, I’ll use single quotation marks for declaring strings.

Handling strings with apostrophes

Strings with apostrophes (like it’s) can be created with single quotation marks if you add a backslash \ before the apostrophe.

'It\'s my birthday today!'

They can also be created with backticks (``). Strings created with backticks are called template strings. You’ll learn about template strings in a later chapter.

`It's my birthday today!`

Adding Strings together

Strings can be added together with the + operator. When you add strings together, you join the two strings into one. This process is also called String concatenation.

console.log('Super' + 'man')
Strings can be added together
Strings can be added together

Notice how man is added directly after Super?

This is why 1 + 1 can equal 11 in JavaScript. It happens if you concatenate two 1 strings.

console.log('1' + '1')
// 11
Adding two strings of '1' gives a string of '11'
Adding two strings of '1' gives a string of '11'

Number

A Number represents numeric data. In JavaScript, you can create numbers by writing the number directly in its numeric form:

3456789

If you log out the number in Chrome’s console, you’ll see the blue text.

console.log(3456789)
Numbers appear in blue in Chrome's console
Numbers appear in blue in Chrome's console

Adding numbers together

Numbers in JavaScript behave like numbers in Math. If you add 1 + 1, you’d see 2. If you add 20 + 2, you’d see 22.

console.log(1 + 1)
console.log(20 + 2)
Adding numbers would give you what you'll expect to see in Math
Adding numbers would give you what you'll expect to see in Math

Subtracting, multiplying and dividing numbers

You can also subtract, multiply or even divide the numbers with -, * and / respectively.

console.log(22 - 2)
console.log(22 * 2)
console.log(22 / 2)
You can also subtract, multiply and divide numbers
You can also subtract, multiply and divide numbers

You can also find the remainder of a number with the modulus (%) operator.

// This means find the remainder of 22 / 5.
console.log(22 % 5) // 2

Boolean

To understand Booleans, take a look at the light switch in your room. When you flick the switch on, you get light. When you flick the switch off, the light goes off.

Booleans are very much like light switches. They can only be “switched on” (true) or “switched off” (false).

You’ll find out how to use Booleans as we go through the if statement in a later chapter.

console.log(true) // true
console.log(false) // false

If you log out a Boolean in Chrome’s console, you’ll see blue text as well.

Booleans appear in blue in Chrome's console
Booleans appear in blue in Chrome's console

Null and undefined

Null and undefined are two important primitive values you need to know, but learning them now would only cause more confusion. We’ll come back to null and undefined in a later chapter when you’re ready to learn them.

Symbol

A Symbol is a new primitive that comes with ES6. You don’t need to use symbols 99% of the time. They’re meant for a very unique use case that you won’t use throughout this course.

Since we’re not going to use Symbols in this course, I suggest you read more about symbols after you’re done with the course.

BigInt

BigInt stands for Big Integer. It lets you create extremely large numbers (that cannot be supported by Number). Unless you work with huge numbers, you won’t need it. We won’t be using BigInt in the course. You can find out more about BigInt here if you’re interested.

Exercise

Prepare yourself for the next lesson by creating numbers, strings and booleans. Try the following:

  1. Create a String and console.log it.
  2. Add two strings together.
  3. Create a Number and console.log it.
  4. Add 27 and 73.
  5. Subtract 30 from 50.
  6. Multiply 5 and 10.
  7. Divide 100 by 10.
  8. Get the remainder of 500 divided by 3.
  9. Create a Boolean and console.log it.

Answers

  • Create a String and console.log it.
console.log('Hello world')
  • Add two strings together.
console.log('Hello ' + 'world')
  • Create a Number and console.log it.
console.log(27)
  • Add 27 and 73.
console.log(27 + 73)
  • Subtract 30 from 50.
console.log(50 - 30)
  • Multiply 5 and 10.
console.log(5 * 10)
  • Divide 100 by 10.
console.log(100 / 10)
  • Get the remainder of 500 divided by 3.
console.log(500 % 3)
  • Create a Boolean and console.log it
console.log(true)