JavaScript Flashcards
What is SAML
Security Application Mark up Language
function calc() { console.log("inside function") }
calc()
calc() //hoist
function calc() { console.log("inside function") }
var calc = function() { console.log("inside function") };
calc()
calc()
var calc = function() { console.log("inside function") };
Assigning a function to a variable
function calc(){ console.log("inside function") };
var anotherfn = calc; anotherfn();
Adding a return value with “return”
function calc(){ return "inside function"; };
var returned = calc(); console.log(returned);
Adding a return value with using a var
function calc(){ var stringVar "inside function"; return stringVar; };
var returned = calc(); console.log(returned);
Passing data to a function
function calc(number1, number2){ return number1 + number2; };
var returned = calc(10,8); console.log(returned);
Passing data to a function via a variable
function calc(number1, number2){ return number1 + number2; };
var calculator = calc; console.log(calculator(15,2));
If statement “executed”
var condition = true;
if (condition){ console.log('Executed'); }else{ console.log('Not executed'); }
If statement “not executed”
var condition = false;
if (condition){ console.log('Executed'); }else{ console.log('Not executed'); }
Checking multiple conditions with else if
var condition = true; var anotherCondition = false;
if (condition){ console.log('Executed'); }else if (anotherCondition){ console.log('Still Executed'); }else (condition){ console.log('Not executed'); }
Casting “true” with 1 and “false” with 0
var condition = 0; var anotherCondition = 1;
if (condition){ console.log('executed'); }else if (anotherCondition){ console.log('still executed') }else { console.log('not executed'); } => returns "still executed"
“0” is explicitly true, even though only 1 == true and all other numbers are not treated the same as true.
Only 1 and 0 are booleans and the rest are treated as true (compared to 0). Negative values (i.e. -1) are also treated as true. Any text is interpreted/treated as true.
var condition = 0; var anotherCondition = 0;
if (condition){ console.log('executed'); }else if (anotherCondition){ console.log('still executed') }else { console.log('not executed'); } console.log(2==true);
=> returns “still executed” , false
Null condition is treated as false to make sure that a certain number is set correctly.
var condition = null; var anotherCondition = 0;
if (condition){ console.log('executed'); }else if (anotherCondition){ console.log('still executed') }else { console.log('not executed'); } console.log(2==true);
=> returns “not executed” , false
Switch statements vs. lots of “else if”. More convenient if we know the cases we need to solve for. It’s much more structured than reading lots of else if statements.
Also, without the break statements, you get all 3 values since it falls through. Once it finds it’s case, it continues to execute.
var luckyNumber = 8;
switch (luckyNumber){ case 1: console.log('Is 1'); break; case 8: console.log('Is 8'); break; default: console.log('Default'); break; }
vs.
var luckyNumber = 8;
if (luckyNumber ==1){ console.log('Is 1'); }else if (luckyNumber ==8){ console.log('Is 8') }else { console.log('Default') }
For loop
- define a variable.
- follow by semicolon
- define parameters of how long it will run
- counter
for (var i=0; i < 5; i++){ console.log(i) }
===> prints 0 through 4 (runs 5 times)
Nested For Loop
- We just j since i is already taken
- The inner loop is executed upon each iteration of the outer loop and before the inner loop isn’t finished, the outer loop won’t continue.
for (var i=0; i < 5; i++){ for (var j=0; j<2; j++) console.log(i*j) }
For loop with an if condition
for (var i = 0; i < 5; i++){ if (i == 0){ console.log('1'); } } ===> prints "1"
for (var i = 0; i < 5; i++){ if (i == 1){ continue; } console.log(i); }
==> 0, 2,3,4
For loop with a conditional if statement and continue statement. This should tell JS that once it meets the condition in the if statement, it should continue executing the for loop
for (var i = 0; i < 5; i++){ if (i == 1){ break; } console.log(i); }
==> 0
for (var i = 0; i < 5; i++){ for (var j = 0; j < 2; j++){ if (i == 1){ continue; } } console.log(i); }
==> 0,1,2,3,4
This continues statement is trigger if i is 1, which is the case. Upon the second iteration, will refer to this inner loop. So this inner loop will continue which doesn’t really matter because we weren’t executing code here anyways.
for (var i = 0; i < 5; i++){ for (var j = 0; j < 2; j++){ if (i == 1){ break; } console.log('inside inner loop, j = ' + j); } console.log(i); }
The break is only applied to the inner loop. Therefore, “continue and “break” only refers to the lop in which they are written and not to any other wrapping loop around them.
inside inner loop, j = 0 inside inner loop, j = 1 0 1 inside inner loop, j = 0 inside inner loop, j = 1 2 inside inner loop, j = 0 inside inner loop, j = 1 3 inside inner loop, j = 0 inside inner loop, j = 1 4
for (var i = 2; i > 1; i--){ console.log(i); }
==> 2
We only make 1 round because we start with 2, subtract 1 and we are at 1 so i > 1 is no longer satisfied.
for (var i = 4; i > 2; i--){ console.log(i); }
==> 4,3
It subtracts 4 until the condition of i > 2 is met. Here, 3 and 4 are greater than 2.