JavaScript - Code Academy Flashcards
(32 cards)
Access a single dimensional array element
array[index]
Create a single dimensional array
var arrayName = [element1, element2, …, elementN]
var myArray = new Array(45 , “Hello World!” , true , 3.2 , undefined);
Create a multi dimensional array
var arrayName = [[1, 2, 3], [1, 2, 3,], [1, 2, 3]]
Create an array using an array constructor
var stuff = new Array();
stuff[0] = 1; stuff[1] = 2;
Access a nested array element
array[index][index]
Boolean literals
True
False
Boolean logical operators
expression1 && expression2 //returns true if both the expressions evaluate to true
expression3 || expression4 // return true if either one of the expression evaluates to true
!expression5 // returns the opposite boolean value of the expression
EXAMPLES
if ( true && false )alert("Not executed!"); //because the second expression is false
if( false || true )alert("Executed!"); //because any one of the expression is true
if( !false )alert("Executed!"); // because !false evaluates to true
Boolean comparison operators
x === y // returns true if two things are equal
x !== y // returns true if two things are not equal
x = y // returns true if x is greater than or equal to y
x y // returns true if x is greater than y
Boolean - == vs. ===
A simple explanation would be that == does just value checking ( no type checking ) , whereas , === does both value checking and type checking .
expression == expression
expression === expression
EXAMPLES
‘1’ == 1 //true (same value)
‘1’ === 1 // false (not the same type)
Single line comment
//
Multi-line comment
/*
Text
text
*/
Print text to console
console.log(‘text’);
Console timer
This function starts a timer which is useful for tracking how long an operation takes to happen.You give each timer a unique name, and may have up to 10,000 timers running on a given page.
console.time(timerName);
Writing a function
A function is a JavaScript procedure—a set of statements that performs a task or calculates a value.
function functionName(argument1, argumentN){ statement1; statement2; statement3; }
Calling a function
functionName(argument1, argument2, …, argumentN);
greet("Anonymous"); // Hello Anonymous!
Function hoisting
The two ways of declaring functions produce different results. Declaring a function one way “hoists” it to the top of the call, and makes it available before it’s actually defined.
hoistedFunction(); // Hello! I am defined immediately!
notHoistedFunction(); // ReferenceError: notHoistedFunction is not defined
function hoistedFunction () { console.log('Hello! I am defined immediately!'); }
var notHoistedFunction = function () { console.log('I am not defined immediately.'); }
If statemetns
“If” statements simply state that if this condition is true , do this , else do something else ( or nothing ) . It occurs in varied forms.
if (condition1) { statement1; } else if (condition2) { statement2; } else { statement3; }
For loops
You use for loops, if you know how often you’ll loop. The most often used varName in loops is “i”.
for ([var i = startValue];[i
While loops
You use while loops, if you don’t know how often you’ll loop.
while (condition) { // Your code here }
EXAMPLE
var x = 0;
while (x
Do while loops
You use do while loops, if you have to loop at least once, but if you don’t know how often.
do { // Your code here } while (condition);
var x = 0; do { console.log(x); // Prints numbers from 0 to 4 x++; } while (x
Math - Returns a random number between 0 and 1
Math.random();
Math - Returns the largest number less than or equal to a number
Math.floor(expression)
Math - Returns base raised to exponent
Math.pow(base, exponent)
Math - Returns smallest integer greater than or equal to a number
Math.ceil(expression)