JavaScript - Basic Flashcards
pop() Method
remove the last element of an array
Example:
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.pop();
Result:
Banana,Orange,Apple
push() Method
Add a new item to the end of an array
Example:
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.push(“Kiwi”);
Result:
Banana,Orange,Apple,Mango,Kiwi
join() Method
Joins the element of an array into a string
Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; var energy = fruits.join();
Result:
Banana,Orange,Apple,Mango
reverse() Method
Reverse the order of the elements in an array
Example:
var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fruits.reverse();
Result:
Mango,Apple,Orange,Banana
split() Method
Splits string into an array of substrings
Example: var str = "How are you doing today?"; var res = str.split(" ");
Result:
How,are,you,doing,today?
round() Method
Math.round(x) (aka round() Method)
Example:
Math.round(2.5);
Result:
3
parseInt() Function
The parseInt() function parses a string and returns an integer.
Example:
a) var c = parseInt(“10.33”) + “<br></br>”;
b) var d = parseInt(“34 45 66”) + “<br></br>”;
Result:
10
34
n!
Factorial. An example is:
5! = 1 * 2 * 3 * 4 * 5 = 120
function factorialize(num) { var factorial = 1; for (var i = 2; i
String.replace() Method
Return a string where “Microsoft” is replaced with “W3Schools”:
var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "W3Schools");
The result of res will be:
Visit W3Schools!
String.toLowerCase() Method
Convert the string to lowercase letters:
var str = "Hello World!"; var res = str.toLowerCase();
The result of res will be:
hello world!
String slice() Method
Extract parts of a string:
var str = "Hello world!"; var res = str.slice(1,5);
The result of res will be:
ello
RegExp Object
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform pattern-matching and “search-and-replace” functions on text.
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
switch statement
[JS Control Flow] Switch allows you to preset a number of options (called cases), then check an expression to see if it matches any of them.
switch (/*Some expression*/) { case 'option1': // Do something break; case 'option2': // Do something else break; case 'option3': // Do a third thing break; default: // Do yet another thing }
What will the following expression return:
true && false;
False
true && true; // => true
true && false; // => false
false && true; // => false
false && false; // => false
What will the following expression return:
false || true;
True
true || true; // => true
true || false; // => true
false || true; // => true
false || false; // => false