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.
Create an object called “friends” and create two objects inside of that named “bill” and “steve”.
Each friend will need a first name, last name, number and address.
Make sure the address is an array.
var friends = { bill: { firstName: 'bill', lastName: 'smith', number: '111-111-1111', address: ['One Apple Drive', 'Cupertino', 'CA', '12345'] }, steve: { firstName: 'steve', lastName: 'ferguson', number: '22-222-2222', address: ['One Facebook Way', 'Palo Alto', 'CA', '54321'] } };
Create a function that will print out all entries in an object called “friends”.
var list = function(friends) { for (var i in friends) { console.log(i); } }
What is a for/in loop look like and what does it do?
for (var key in object) { // Access that key's value // with object[key] }
*the “key” bit can be any placeholder name you like. It’s sort of like when you put a placeholder parameter name in a function that takes arguments.
Definite Object Methods
Object methods are actions that can be performed on objects.
Object properties can be both primitive values, other objects, and functions.
An object method is an object property containing a function definition
const
ES6. const means that the variable can’t be reassigned.
In other words, const
is a signal that the variable won’t be reassigned.
let
ES6. let
, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm.
It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.