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
What will the following expression return:
!false; // => true
True
!true; // => false
!false; // => true
Create an array called ‘list’ with 3 items in it.
var list = [“errands”, “cleaning”, “homework”];
Write the code to log to the console each item of the following array:
var languages = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”];
for (var i = 0; i
What is a heterogeneous array?
An array that contains a mixture of data types like:
var mix = [42, true, “towel”];
What is a two-dimensional array?
An array inside an array, in this case an array nested one layer deep:
var twoDimensional = [[1, 1], [1, 1]];
What is a jagged array?
An array that includes arrays of different lengths e.g.
var jagged = [[“name”, “gender”, 30], [“name”, “gender”], 33, true];
Write an example of object literal notation by creating the object “me” and giving it two keys.
var me = { name: "bubba", age: 31 }
Create a new object called “myObj” using the object constructor. Then, assign a name to myObj using the shorthand version.
var myObj = new Object();
myObj.name = “Bubba”;
for key in object
for key in object describes the iteration through all the keys of an object. Because this is not sequentially indexed, in is the control. Objects generally have a finite number of properties in them, and this loop susses them out, one at a time.