Web Programming Exam 2
What does array.push(value) do?
Adds a value to the end of the array
What does splice(startingIndex, totalElementsToDelete, valuesToAdd) do?
Adds or removes elements fromanywhere in the array and returns thedeleted elements (if any)
|| has higher precedence than && (T/F)
FALSE. && has higher precedence than ||
What is dynamic-typing?
JavaScript uses dynamic typing, which determines a variable's type at run-time
What is hoisting?
JavaScript's behavior of moving variable declarations to the top of the current scope.
The _____________ method returns a number with n decimal places.
Number.toFixed(n)
The _____________ method returns an array of an object's property names and is often used to determine the number of elements in an associative array.
Object.keys()
What does array.shift( ) do?
Removes the first array element and returns the element
What does array.pop( ) do?
Removes the last array element and returns the element
What is the split() method?
Returns an array of strings formed by splitting the string into substrings.
What is the trim() method?
Returns the string with leading and trailing whitespace removed.
What is the substr() method?
Returns the substring that begins at a given index and has a given length. substr(idx, length)
"2" * 3 = 2 * 3 = 6. (T/F)
TRUE
"2" + 3 = "2" + "3" = "23". (T/F)
TRUE
Array elements that are not assigned a value are undefined. (T/F)
TRUE
Arrays may be of the same types or different types (T/F)
TRUE
Because function expressions are not hoisted, using a variable before the variable is assigned to a function expression causes an exception. (T/F)
TRUE
sort()'s default behavior is to sort each element as a string using the string's Unicode values (T/F)
TRUE. this may yield unsatisfactory results.
How does the comparison function that is passed into the sort ( ) method work?
The comparison function returns a number that helps sort() determine the sorting order of the array's elements: Returns a value < 0 if the first argument should appear before the second argument. Returns a value > 0 if the first argument should appear after the second argument. Returns 0 if the order of the first and second arguments does not matter.
What is a JavaScript variable's scope?
The context in which the variable can be accessed.
What is do while loop ?
The do-while loop executes the loop body before checking the loop's condition to determine if the loop should execute again, repeating until the condition is false.
What is a for each loop?
The forEach() method takes a function as an argument. The function is called for each array element in order, passing the element and the element index to the function.
What does the replace( ) method do? How is it used?
The replace() method replaces one string with another and returns the string with the replacement string inside. var s = "Seek and you will find."; s = s.replace("find", "discover"); This changes the string to: "Seek and you will discover"
What happens if parseInt() or parseFloat() are passed in something that is not a number?
They return NaN
What is an object literal or object initializer?
a comma-separated list of property name and value pairs.
What is a getter?
a function that is called when an object's property is retrieved
What is a setter?
a function that is called when an object's property is set to a value
What is an anonymous function?
a function without a name
The String method ___________ returns the character at the specified index as a string
"text".charAt(#)
How to create a self-invoking function?
(function( ) { console.log("Hello, Anonymous!"); })( );
What is an object property?
a name-value pair, where the name is a string and the value is any data type
What is a template literal?
a string literal enclosed by the back-tick (`) that allows embedding expressions with a dollar sign and braces (${expression}). Ex: `test ${1 + 2}` evaluates to "test 3".
What is the map object?
alternative to the associative array for storing key/value pairs
What is a self-invoking function?
an anonymous function that invokes (calls) itself
What is an arrow function?
an anonymous function that uses an arrow => to create a compact function.
What is the global object?
an object that stores certain global variables, functions, and other properties.
What is an object?
an unordered collection of properties
The __________ statement breaks out of a loop prematurely.
break
How is JavaScript executed?
by an interpreter
What is a switch statement?
compares an expression's value to several cases using strict equality (===) and executes the first matching case's statements. If no case matches, an optional default case's statements execute.
What does a conditional (ternary) operator look like?
condition ? expression1 : expression2
Output may be produced using the function ___________ , which displays text or numbers in the console.
console.log()
The _________ statement causes a loop to iterate again without executing the remaining statements in the loop.
continue
What is an associative array?
data structure that maps keys to values
How to delete from an associative array?
delete array["nameYouWantToDelete"]
What is the structure of a do-while loop?
do { body } while (condition);
"3" === 3 (T/F)
false because "3" is a string, and 3 is a number.
What is the structure of a for loop?
for (initialization; condition; finalExpression) { body }
How to construct a for-of statement?
for (var item of groceries) { console.log(item); }
A ___________ ___________ is identical to a function declaration, except the function name is omitted.
function expression
When running JavaScript code in a web browser, global variables are usually assigned as properties to the ______ ______ ______.
global window object -so global variable called test is accessible as window.test
How is the for each method structured?
groceries.forEach(function(item, index) { console.log(index + " - " + item); });
The _____________ method returns the index of the search string's first occurrence inside the String object or -1 if the search string is not found.
indexOf()
________________ searches from the beginning of the array to the end. _____________________ searches from the end of the array to the beginning. Both functions take two arguments: searchValue and startingPosition
indexOf(); lastIndexOf()
he JavaScript function __________ returns true if the argument is not a number, false otherwise
isNaN()
The ______________ method returns the index of the search string's last occurrence inside the String object or -1 if the search string is not found.
lastIndexOf()
A variable declared inside a function has _____ scope, so only the function that defines the variable has access to it. A variable declared outside a function has ______ scope, and all functions have access to it.
local scope ; global scope
What is the console?
location where text output is displayed.
What functions convert strings into numbers?
parseInt() and parseFloat()
The _______ function prompts the user with a dialog box that allows the user to type a single line of text and press OK or Cancel. What happens if OK is pressed? Cancel?
prompt() returns the string the user typed if pressed OK or null if the user pressed Cancel.
For a map object, the ________ method sets a key/value pair, and the ________ method gets a key's associated value.
set(), get()
__________ returns the string converted to lowercase characters.
str.toLowerCase()
__________ returns the string converted to uppercase characters.
str.toUpperCase()
What is the structure of a switch statement?
switch (expression) { case value1: break; case value2: break; ... default: // Statements executed when no cases match }
Two operands are strictly equal if
the operands' data types and values are equal.
How to declare a function expression?
// Function name is omitted var displaySum = function(x, y, z) { console.log(x + y + z); } // Function call displaySum(2, 5, 3);
How to write comments in JavaScript?
// Single line comment /* Multi-line comment */
What are the 3 rules identifiers must follow in JavaScript?
1. An identifier can be any combination of letters, digits, underscores, or $. 2. An identifier may not start with a digit. 3. An identifier may not be a reserved word like var, function, or while.
Difference between var and let inside a function?
A variable declared inside a function with var is scoped to that function: the variable is accessible anywhere within the function, but not outside. A variable declared inside a function with let has block scope: the variable is accessible only within the enclosing pair of braces.
Difference between var and let outside a function?
A variable declared using var or let that is not inside a function creates a global variable that is accessible from anywhere in the code.
What does array.unshift(value) do?
Adds a value to the beginning of the array
3 == "3" (T/F)
true because "3" is converted to 3 before the comparison, and 3 and 3 are the same.
How to create a map object?
var capitals = new Map( )
How to structure an arrow function?
var func = (parm1, parm2) => return expression;
3 cases when assigning to a global variable X:
1. Declared with var: in which case a property named "X" is added to the global object. 2. Declared with let, in which case a property named "X" is not added to the global object, but X is still accessible from anywhere in the code. 3. X has not been declared with var or let, in which case the variable becomes a property of the global object, even if assigned to inside a function.
What are the JavaScript data types? (7)
1. Number 2. String 3. Boolean 4. Array 5. Object 6. Null (Intentionally absent of any object value) 7. Undefined (Variable without a value)
Syntax to define getter and setters in JavaScript?
1. get property() { return someValue; } 2. set property(value) { ... }
How is a constant declared in JavaScript?
A JavaScript constant is declared with the const keyword and is usually named with an identifier in all capital letters
What is an identifier?
A name created by a programmer for an item like a variable or function is called an identifier.
What is an interpreter?
A program that runs code one line at a time. Instead of converting all of the code at once it runs each line as it's needed. It interprets that specific line from your code to computer code.