JavaScript Flashcards
(120 cards)
• What is the purpose of a boolean?
To give us true or false values. -if this-then this
or not this
• What does the = operator mean in JavaScript?
Assigns a value to a variable
What does string concatenation mean?
take two strings and adding them
what does the + plus operator do?
+= takes right value and adds to left variable and new value is asigned to left variable
• What data type is returned by an arithmetic operation?
number
• What data type is returned by comparing two values (, ===, etc)?
boolean
• Describe the parts of a function call.
Function name, parenthesis, and any arguments
Why are parameters useful?
You can think of a parameter as a placeholder. It is basically a variable whose value is not known until we call the function and pass an argument. When the function’s code block is run, the parameter is will be holding the value of the argument. Here’s an example of passing the string “friend” as an argument to sayHello.
• What two effects does a return statement have on the behavior of a function?
a return stops the function from running further.
return xxx;
you can’t write more code after
and it has a return value if you run it
Causes the function to produce a value we can use in our program.
Prevents any more code in the function’s code block from being run.
what does iteration mean?
single run of the loops code black?
• When does the condition expression of a while loop get evaluated?
• When does the initialization expression of a for loop get evaluated?
Before the loop begins
Before Anything
when does the condition expression of the for loop get evaluated?
before each iteration, after initialization.
• When does the final expression of a for loop get evaluated?
after the last iteration
• Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
Break
splice()
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)
slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
slice()
slice(start)
slice(start, end)
Math.random()
Return value
A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive).
Math.floor(Math.random() * x)
function getRandomInt(max) { return Math.floor(Math.random() \* max); }
console.log(getRandomInt(3)); // expected output: 0, 1 or 2
.unshift(x)
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // expected output: 5
console.log(array1); // expected output: Array [4, 5, 1, 2, 3]
shift()
The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array. const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1
split(x)
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.
split()
split(separator)
split(separator, limit)
const str = ‘The quick brown fox jumps over the lazy dog.’;
const words = str.split(' '); console.log(words[3]); // expected output: "fox"
const chars = str.split(''); console.log(chars[8]); // expected output: "k"
const strCopy = str.split(); console.log(strCopy); // expected output: Array ["The quick brown fox jumps over the lazy dog."]
Loop iteration (FOR)
You can think of a loop as a computerized version of the game where you tell someone to take X steps in one direction, then Y steps in another. For example, the idea “Go five steps to the east” could be expressed this way as a loop:
for (let step = 0; step \< 5; step++) { // Runs 5 times, with values of step 0 through 4. console.log('Walking east one step'); }
For Loops
A for statement looks as follows:
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
Copy to Clipboard
When a for loop executes, the following occurs:
The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
The conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. Otherwise, the for loop terminates. (If the conditionExpression expression is omitted entirely, the condition is assumed to be true.)
The statement executes. To execute multiple statements, use a block statement ({ … }) to group those statements.
If present, the update expression incrementExpression is executed.
Control returns to Step 2.