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:
- String
- Number
- Boolean
- Null
- Undefined
- Symbol
- 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!')
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')
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
Number
A Number represents numeric data. In JavaScript, you can create numbers by writing the number directly in its numeric form:
3456789If you log out the number in Chromeโs console, youโll see the blue text.
console.log(3456789)
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)
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 find the remainder of a number with the modulus (%) operator.
// This means find the remainder of 22 / 5.console.log(22 % 5) // 2Boolean
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) // trueconsole.log(false) // falseIf you log out a Boolean in Chromeโs console, youโll see blue text as well.
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:
- Create a
Stringandconsole.logit. - Add two strings together.
- Create a
Numberandconsole.logit. - Add
27and73. - Subtract
30from50. - Multiply
5and10. - Divide
100by10. - Get the remainder of
500divided by3. - Create a
Booleanandconsole.logit.
Create a String and console.log it
console.log('Hello world') // Hello worldAdd two strings together
console.log('Hello' + 'world') // Helloworldconsole.log('Hello' + ' ' + 'world') // Hello worldCreate a Number and console.log it
console.log(27) // 27Add two numbers together
console.log(27 + 73) // 100Subtract one number from another number
console.log(50 30) // 20Multiply two numbers
console.log(5 * 10) // 50Divide one number by another number
console.log(100 / 10) // 10Get the remainder of 500 divided by 3.
console.log(500 % 3) // 2Create a Boolean and console.log it
console.log(true) // trueWelcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson