JavaScript Flashcards
Accessing array elements
array[index]
Example:
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
primes[0]; // 2
primes[3]; // 7
primes[150]; // undefined
Array literals
var arrayName = [element0, element1, …, elementN]
Example:
var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37];
var junk = [10, “Hello”, Math, console, { lots: “of different types” }];
Multi-dimensional arrays
var multidimensionalArray = [[1,2,3],[4,5,6],[7,8,9]] // two dimensions, 3x3
Array constructor
Example 1:
var myArray = new Array(45 , "Hello World!" , true , 3.2 , undefined); console.log(myArray);
Example 2:
var stuff = new Array();
stuff[0] = 34;
stuff[4] = 20;
stuff // [34, undefined, undefined, undefined, 20]
Accessing nested array elements
array[index][index]….
Example:
var myMultiArray = [
[1,2,3,4,5, [1,2,3,4,5] ],
[6,7,8,9,10 , [1,2,3,4,6] ],
[11,12,13,14,15 , [1,2,3,4,5] ],
[16,17,18,19,20, [1,2,3,4,5] ]
];
console.log( myMultiArray[1][5][4] ); //Outputs 6 , the value in the last element of the last element of the second element of myMultiArray.
Boolean literals
true
false
Boolean logical operators
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
!!true // remains true
Operator Precedance
// Brackets - have the highest precedence // ! - lower than Brackets // && - lower than ! // || - the lowest
if(true && !!false || true)alert(“Guess again ??”);
/* Executed , here is the evaluation process-
true && !!false || true - becomes
true && false || true - (no brackets present , so ! evaluated ) becomes
false || true - (then && evaluated) which becomes true */
Operator Associativity
Determines the order in which operators of the same precedence are processed. For example, consider an expression: a * b * c . Left-associativity (left-to-right) means that it is processed as (a * b) * c, while right-associativity (right-to-left) means it is interpreted as a * (b * c). */
// Brackets , && , || have left to right associativity // ! has right to left associativity // So ,
!false && !!false //false // evaluated in the manner - !false && false - true && false - false
Comparison operator
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 less than or equal to y
x >= y // returns true if x is greater than or equal to y
x < y // returns true if x is less than y
x > y // returns true if x is greater than y
Other ways of deriving true and false
if(1)console.log(“True!”); // output True! , since any non-zero number is considered to be true
if(0)console.log(“I doubt if this gets executed”); // not executed , since 0 is considered to be false
if(“Hello”)alert(“So, any non-empty String is also true.”); //Gets executed
if(““)alert(“Hence , an empty String is false”); // Not executed
Difference between == and ===
== does just value checking ( no type checking ) , whereas , === does both value checking and type checking . It is always advisable that you never use == , because == often produces unwanted results
Example:
‘1’ == 1 //true (same value)
‘1’ === 1 // false (not the same type)
true == 1 // true (because 1 stands for true ,though it’s not the same type)
true === 1 // false (not the same type)
Comments
// Single line
/*
Multi
line
*/
console.log
Prints to the console, good for debugging.
Example:
console.log(‘Poker night!’);
console.time
console. time(timerName);
console. timeEnd(timerName);
Example:
console.time(“My Math”);
var x = 5 + 5;
console.log(x);
console.timeEnd(“My Math”);
console.log(“Done the math.”);
/* Output:
10
My Math: (time taken)
Done the math.
function
function name(argument1 , argument2 …. argumentN){
statement1;
statement2;
statementN;
}
Example:
function greet(name) { return "Hello" + name + "!"; }
function calling
functionName(argument1, argument2, …, argumentN);