JS - Udemy Flashcards
ES6 def & features
Var, arrow functions?
How to get to & run code in console?
- Control+Shift+C or Inspect or More Tools; -
What is modulo? + ex
- %, returns remainder; - 25 % 5 = 0
How to notate exponents?
5**2 returns 25
What is the order of operations?
PEMDAS - parentheses, exponents, mult, div, add, sub
What is NaN & what will return it?
0/0; NaN + a number;
How does the browser read JS?
- HTML file, include
Quick way to view .js in browser?
- Open html file in file explorer that contains .js tag
What’s an Unary Operator? Ex.
- ++ (existingVar+=1); – (existingVar-=1);
What are the primitive types?:
- Number, String, Boolean, Null, Undefined
Booleans
- true & false; - 1 or 0
What does null mean?
- Absence of any value
What does Undefined mean?
- Variable with no assigned value
List the falsey values
false, 0, “”, null, undefined, NaN
How to get the index (content) of a string/letter? ex.
string[1] = t
How to convert a string to upper? ex
string.toUpperCase( )
How to convert a string to lower? ex
string.toLowerCase( )
What does Trim do? ex.
” string “.trim = “string”
How to get the index (letter) of a string? ex
string.indexOf(“tr”) = 1
How to return a fragment of a string? ex
string.slice(0,2) = “str”
How to substitute part of a string? ex
string.replace(‘g’, ‘z’) = “strinz”
How to keep a quote or backslash in a string from fucking up your day? ex
- Escape Characters; - / preceding a “ or ‘ or /
How to insert expressions, variables etc. into a string? ex
- Template Literals –NOT QUOTES! TILDE; -
String ${3+4} ${var}
What is the Math object? ex
- Math. contains methods
What are a few Math objects?
- Math.round(4.9) = 5; .floor, .abs, .pow, .random
What returns the data type?
typeof ___
How to parse numbers from a larger string?
- parseInt; - parseFloat;
What’s the difference between == and ===?
` === checks for equality AND type
IF, ELSE IF, ELSE: EX: Write a function - if x is > 10 return “>10”, else if x > 6 return that, else returns < 6
"if (x > 10) { console.log('>10'); } else if (x < 10 && x > 6) { console.log('>6'); } else { console.log('<6') }"
What is ! Ex
- Reverses truthiness; - !(3 < 4) = false
What is the order of Logical Operators?
!, &&, then || (notwithstanding parentheses)
EX: What conditional is best when you have one thing you’re checking against a set of values?
"switch (day) { case 1: return ""Monday""; break; case 2: return ""Tue""; default: return ""else value here""; }
use break for each case you don’t want to stop - default is ““else”””
What’s another way of writing an IF/ELSE statement?
”- Ternary Operator; -
num = 6
num === 7 ? console.log(““lucky!””) : console.log(““BAD!””); = BAD!”
JS array format?
let or const arrName = [0,1,2,3]
What are the 4 basic Array Methods?
- Push (add to end) , Pop (remove from end), Shift (remove from start), Unshift (add to start)
How to join arrays?
old.concat(new)
Arr method to search for a value?
arr.includes(‘string’)
Arr method to reverse array?
arr.reverse()
Arr method to combine elements?
arr.join(‘-‘)
Arr method to copy portion of array?
arr.slice(start, end+1)
Arr method to remove/replace elements?
arr.splice(startindex[deletecount[item1,item2,…
Arr method to sort array?
arr.sort(compareFunction)
What happens when you use const w/ arrays?
Elements can change, “Arr =” cannot be invoked
What happens when you use const w/ arrays?
Elements can change, “Arr =” cannot be invoked
EX: fitBitData obj
"const fitBitData = { totalSteps : 588, totalMiles : 2.3, avgSleep : '2:13' };"
How to access an Object Property?
obj[key] = value (key = number or ‘string’)
EX: How to nest arr + obj in obj?
"const student = { name : 'Jon', strengths : ['Music','Art'], exams : { midterm : 92, final : 88 } };"
EX: For Loop
“for(initial value[i]; condition; increment)
for(let i = 1; i <=10; i++){ console.log('hello'); }"
EX: How to loop over array? (Like each do | |?)
"for(let i = 0; i < array.length; i++) { }"
EX: While loop
“while(somethingIsTrue) {
action;
counter++;
}”
EX: For..Of
”- Used for iterating through arrays
for (let ELEMENT of ARRAY) {
statement
}”
For..In
”- Used for iterating through obj
for (let MOVIE in moviesObject) {
statement
}”
JS ver. of Each_With_Index? ex.
“array.forEach(function (element, index) {
});”
What to use instead of forEach when you want to stop looping upon solution? ex.
”- For loop with counter:
for (let i = 0; while i \_\_\_ condition; i +=1) { }"
How to create a hash table from arr? ex.
"let values = [4, 2, -1, 5, 6]; let hashTable = {};
values.forEach((value, index) => hashTable[value] = index);
hashTable[-1]; // 2
hashTable[6]; // 4
“
What does the “break” keyword do in a loop?
- Stops loop in a while loop; - diff from switch?