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.") }
else statement example
if(false) { This won't run } else { This will run }
if(isLoggedIn) { console.log("User is logged in.") // show profile page } else { console.log("User is not loggedf in.") /// show sign in page }
switch statemnet example
switch(variable) { case value 1: code to run if variable is equal to value 1 break case value 2 code to run if variable is equal to value 2 break }
let currentPage = “home”
switch(currentPage) { case 'home': console.log('Display the homepage.') //this will run break case 'profile'" console.log('Display the profile page.') //this will not run break }
switch default statement example
switch(variable) {
case value 1:
code to run if variable is equal to value 1
break
case value 2:
code to run if variable is equal to value 2
break
default:
code to run if variable doesn’t equal value 1 or 2
break
}
let currentPage = “foo”
switch(currentPage) { case 'home': console.log('Display the homepage.') //this will not run break case 'profile': console.log('Display the profile page.') //this will not run break default: console.log('Display a "page not found" error.') //THIS WILL RUN! HOORAY break }
Array
a variable that stores multiple elements
Concatenation
combining multiple strings
Iteration
Repeating a process (lines of code)
Helpful for processing every element in an array
What is a For Loop & The Anatomy of a For Loop
A way to iterate. Loops can run code a number of times
Initializer
Condition
Aferthought
for(let x = 0; x < 4; x++) { console.log(x) }
Initializer
Runs before the loop! Tells us where to start the loop
Condition
Boolean expression that stops the loop when the answer is false. Without this, it would go on forever.
Afterthought
Runs at the end of each cycle in the loop. It tells the code how to move from the start of the loop to the next piece
Body (in forLoops)
The code to execute until the condition is false
let card = { number: 9 }
identify the object, the property, and the value
object: card
property: number
value: 9
Functions and their properties
a block of code designed to perfom task funtions must have - function name - pair of parentheses - pair of curly braces
Calling a function
To call a function, write only the name with a pair of parenthese
function sayHi ( ) { console.log("Hello!") }
sayHi ( ) is the function
Parameters
allow inputs to the function
you can have as many as you want
used to put new value inside parenthese when calling a function
Return Statements
the function stops executing and goes back to where the function was called
Scope
Global vs Local
The place in your code where a variable is defined
Global: The scope that contains all other scopes; all variables defined here are visible in all contained scopes
Local: An isolated scope typically defined by curly braces. All variables defined here are only visible within the scope itself
Dot notation
A way to access properties of an object using “.”