1. Number: Integers like 1, 2, 3, or math like 1 + 2 (returns 3) or 6 / 2 (returns 3). There’s also something called a modulo calculation, which I’ve explained below if you’re unfamiliar with the concept (I was super confused by it when I first saw it).
Note: % = Modulo. Modulo is a calculation of what is remaining after you divide one number by another. So 3 % 2 = 1, because if you divide 3 by 2, you end up with 1 left over. 12 % 6 = 0, because 12 is able to be divided up into two buckets of 6, with nothing remaining.
2. String: Text, indicated by putting something in single or double quotes. “hello world” ‘bob’, “Hello ” + “there” returns “Hello there” (don’t forget the space after the ‘o’!). You can also turn numbers into strings: “3”.
Note: If your string contains a quote, there are a couple ways to handle it:
a) Use double quotes on the outside, and single quotes on the inside: “This isn’t going to return an error.”
b) Use a backslash (\) to indicate that the single quote within the string should not be considered code: ‘This isn\’t going to return an error.’ (The \ doesn’t print)
Performing calculations with strings:
Example 1: Adding a number and a “number” (a number with type equal to string)
Input: 10 + “34”
Example 2: Subtracting a “number” from a number
Input: 10 – “3”
3. Boolean: True/False (example: 3 > 4 would return False, 3 >= 3 would return True)
5 > 10
5 < 10
5 <= 5
2 = 2
2 === 2 (does 2 equal 2?)
3 !== 3 (is 3 not equal to 3?)
Q: Ok, I’m with you, except for the triple equal signs. What’s up with that?
1) Must start with a letter, underscore or dollar sign (cannot be a number or any other symbol)
2) Variable names should be in camel case (exampleVariable, newBook, badApple)
Example of using variables with prompts:
var currentJob = prompt(“Enter your current job title”);
var dreamJob = prompt(“Enter your dream job title”);
After user has input:
Output: “Web Developer”
Another example, this time with numbers input as variable values:
var luckyNumber = prompt(“What is your lucky number?”);
var unluckyNumber = prompt(“What is your least lucky number?”)
After user has input:
Output: “2”(See the quotes? Those are strings!)
In order to turn a string back into a number, we’d change the Type: