JavaScript - Code Academy Flashcards Preview

JavaScript > JavaScript - Code Academy > Flashcards

Flashcards in JavaScript - Code Academy Deck (32)
Loading flashcards...
1

Access a single dimensional array element

array[index]

2

Create a single dimensional array

var arrayName = [element1, element2, ..., elementN]

var myArray = new Array(45 , "Hello World!" , true , 3.2 , undefined);

3

Create a multi dimensional array

var arrayName = [[1, 2, 3], [1, 2, 3,], [1, 2, 3]]

4

Create an array using an array constructor

var stuff = new Array();

stuff[0] = 1;
stuff[1] = 2;

5

Access a nested array element

array[index][index]

6

Boolean literals

True
False

7

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

8

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

9

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)

10

Single line comment

//

11

Multi-line comment

/*

Text
text

*/

12

Print text to console

console.log('text');

13

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);

14

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;
}

15

Calling a function

functionName(argument1, argument2, ..., argumentN);

greet("Anonymous");
// Hello Anonymous!

16

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.');
}

17

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;
}

18

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

19

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

20

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

21

Math - Returns a random number between 0 and 1

Math.random();

22

Math - Returns the largest number less than or equal to a number

Math.floor(expression)

23

Math - Returns base raised to exponent

Math.pow(base, exponent)

24

Math - Returns smallest integer greater than or equal to a number

Math.ceil(expression)

25

Math - Returns square root of a number

Math.sqrt(expresssion)

26

Numbers - Returns the remainder left after dividing the left hand side with the right hand side.

number1 % number2

27

Numbers - Returns true if the given number is not a number, else returns false.

isNaN([value])

28

Numbers - prefix and post fix increment/decrement

--variable //Prefix Decrement
++variable //Prefix Increment
variable-- //Postfix Decrement
variable++ //Postfix Increment

29

Object literal

{
"property 1" : value1;
property2 : value2;
number: value3;
}

EXAMPLE
var obj = {
name: "Bob",
married: true,
"mother's name": "Alice",
"year of birth": 1987,
getAge: function () {
return 2012 - obj["year of birth"];
},
1: 'one'
};

30

Property access

name1[string]
name2.identifier

EXAMPLE
obj['name']; // 'Bob'
obj.name; // 'Bob'
obj.getAge(); // 24