JavaScript Types & Variables

Notes on what I learned from the JavaScript section of The Complete Web Developer in 2018: Zero to Mastery class on Udemy

Q: What is JavaScript?
A: It’s the programming language that makes things happen on the website you visit. Pop-ups, replying to a Facebook post, tweeting, posting a YouTube video… ALL JavaScript!

Cool fact: JavaScript was created by Netscape in an effort to give it a competitive advantage. (bonus points: who actually remembers Netscape? If you don’t know what it is, go google it. RIP Netscape.)

JavaScript Types (values that JavaScript can have)

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”

Output: “1034”

Why? JavaScript looks at the input (10) and sees that you’re trying to add it to a string, so it makes the entire thing a string to make it work. So if you added “SillyBilly” + “34”, you’d get “SillyBilly34”. It’s treating the 10 like SillyBilly (a string).

Example 2: Subtracting a “number” from a number

Input: 10 – “3”

Output: 7

Q: What the heck?! Why is subtraction treated differently than addition? Why JavaScript gods, WHY?!
A: Sometimes the gods have a reason. Have a little faith. The reason is this – while you can add two strings together (i.e. “hello ” + “there” = “hello there”), you can’t really subtract a string from another string. Because of this, JavaScript helpfully(?) decides to subtract 3 as a number rather than return an error, because it assumes you know what you’re doing (a dangerous assumption in my case, but thanks for the vote of confidence, JavaScript gods!).

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?
A: I’m actually not sure. I’ve seen double equal signs used in Python, but this is my first time seeing triple. I’ll come back and explain it when I understand it. All I know for now is that if you want to ask if something is equal to another thing in JavaScript, you need to use triple equal signs.

JavaScript Variables

Variable rules:
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”);

User is prompted and inputs: “Teacher” and “Web Developer”

After user has input:

Input: currentJob

Output: “Teacher”

Input: dreamJob

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?”)

User is prompted and inputs: “5” and “2”

After user has input:

Input: luckyNumber

Output: “5”

Input: unluckyNumber

Output: “2”

(See the quotes? Those are strings!)

In order to turn a string back into a number, we’d change the Type:

Input: Number(luckyNumber)

Output: 5

Add the numbers together:
var sum = Number(luckyNumber) + Number(unluckyNumber)
Return the sum to the user:

No comments