JavaScript Flashcards
Adding comments to your JavaScript
// for a single line comment
/* this is for a multiline comment */
How to declare value to a variable
var x = 5
here x is the variable and 5 is the value
Printing to the log
console.log()
The five primitive types
Number - Numberical values
String - Characters surrounded by single or double quotes
Boolean - true or false statements
Null - a variable that doesn’t have a value - this is delibrate!! You have to phyically name something null.
Undefined - a variable that is missing a value - NOT delibrate!
infinity and NaN
these are defined as numbers! not strings!!!
NaN means not a number lol
Some operating actions
+ Addition/Concatenation 5 + 4 9
- Subtraction 8 - 1 7
* Multiplication 6 * 7 42
% Modulu (remainder) 10 % 3 1
** Exponent 2**4 16
Adding numbers and Concatenating string
var x = 5 var y = 6 var z = "hello" var a = "world" console.log(x = y) prints 11 console.log(z + a) prings "helloworld" console.log(z + " " + a) prings "hello world"
String Interpolation
Use backticks instead of quotes!!
var name = 'Tris' var age = 7 var sentence = 'I'm ${name} and I'm ${age} !' console.log(sentence) prints "I'm Tris and I'm 7!"
Statements vs Expressions
Statements are instructions
var x = 1000 - this statment defines a value
console.log(‘hi’) - this statement logs a value
Expressions resolves to a value
x == y - expression that evaluates to false
15 + 2 - expression that evaluates to 17
(in a statment you’re telling the computer something. in an expression you’re asking the computer to answer something)
What is a Boolean Expression?
Expressions that resolve to a Boolean data type (true/false)
8 comparison operators
== Abstract equality (can compare things that don’t have the same primitive type
i.e. 1 == “1” where 1 is a number and “1” is a string)
!= Abstract inqueality (same as above but it’s telling you two things are NOT
equal
=== Strict equality (these HAVE to be the same primitive type)
!== Strict inqueality
< Less than
<= Less th an or equal to
> Greater than
>= Greater than or equal to
True or False? Anything that isn’t falsy is truthy
TRUE!!!!
True or False? NaN = NaN
FALSE! NaN means not a number. But they are not neccessarily the same NaN
Think of it like two differnt secrets
Or how a lion and a napkin are both not numbers but a lion isn’t equal to a napkin
if Statement example
if (boolean expression) {
code to run if expression is true
}
if (isLoggedIn) {
console.log(“User is logged in.”)
}
else if statement example
if (false) { This won't run. } else if (true) { This will run. }
if(userAge > 21) { console.log("User is an adult.") } else if(userAge >12) { console.log("User is a teenager.") }