JavaScript Flashcards
What is the purpose of variables?
A variable is a way to store values
How do you declare a variable?
To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon. var x; this variable has been declared but the value is undefined
How do you initialize (assign a value to) a variable?
with = assignment operator
What characters are allowed in variable names?
The period, the underscore, and the characters $, #, and @ can be used within variable names. For example, A. _$@#1 is a valid variable name.
What is the purpose of a string?
To store text data
What is the purpose of a number?
What is the purpose of a boolean?
Boolean data types can have one of two values: true or false, used to create true/false statements.
(Booleans are helpful when determining which part of a script should run.)
What is the difference between null and undefined?
null is an assigned value. It means nothing. undefined means a variable has been declared but not defined yet.
How do you update the value of a variable?
Give five examples of JavaScript primitives.
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties.
There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.
Why is it a good habit to include “labels” when you log values to the browser console?
A console log “label” is simply a short string that describes the variable or value being logged.
Since the console.log() method can write multiple values to the console at the same time, it’s easy to include a “label” when printing a value.
How do you update the value of a variable?
Once you have assigned a value to a variable, you can then change what is stored in the variable later in the same script.
Once the variable has been created, you do not need to use the var keyword to assign it a new value.
var inStock;
var shipping;
inStock = true;
shipping = false;
inStock = false;
shipping =true;
What data type is returned by an arithmetic operation?
Number data type
What is string concatenation?
Combination of two or more strings
What purpose(s) does the + plus operator serve in JavaScript?
Addition operator. Arithmetic operation with numbers and combinations for stings
What data type is returned by comparing two values (<, >, ===, etc)?
Boolean (true or false)
What does the += “plus-equals” operator do?
Adds new variable to the current variable. Changes/reassigned the value
Unary operators
A unary operation is an operation with only one operand.
(delete
The delete operator deletes a property from an object.)
typeof
The typeof operator determines the type of a given object.
+
The unary plus operator converts its operand to Number type.
What are objects used for?
Objects group together a set of variables and functions to create a model of a something you would recognize from the real world. In an object, variables and functions take on new names.
What are object properties?
A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects.
The properties of an object define the characteristics of the object. You access the properties of an object with a simple dot-notation:
Describe object literal notation.
The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after every key:value pair, except for the last, just like a regular array.
What are arrays used for?
An array is a special type of variable. It doesn’t just store one value; it stores a list of values.
Describe array literal notation
The values are assigned to the array inside a pair of square brackets, and each value is separated by a comma. The values in the array do not need to be the same data type, so you can store a string, a number, and a Boolean all in the same array.
colors = [‘white’, ‘black’, ‘custom’];
Describe the parts of a function definition.
The function keyword to begin the creation of a new function.
An optional name. (Our function’s name is sayHello.)
A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)
The start of the function’s code block, as indicated by an { opening curly brace.
An optional return statement. (Our sayHello function doesn’t have a return statement.)
The end of the function’s code block, as indicated by a } closing curly brace.
What is a method?
A method is a function which is a property of an object.
How is a method different from any other function?
A method, like a function, is a set of instructions that perform a task. The difference is that a method is associated with an object, while a function is not.
How do you remove the last element from an array?
Array.prototype.pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
How do you round a number down to the nearest integer?
Math.floor()
The Math.floor() function returns the largest integer less than or equal to a given number.
How do you generate a random number?
Math.random()
The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.
How do you delete an element from an array?
Array.prototype.splice()
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. To access part of an array without modifying it, see slice().
How do you append an element to an array?
Array.prototype.push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.
How do you break a string up into an array?
The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
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.”]
When does the condition expression of a while loop get evaluated?
while
The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
What is the purpose of a loop?
Definition: Loops are a programming element that repeat a portion of code a set number of times until the desired process is complete. Repetitive tasks are common in programming, and loops are essential to save time and minimize errors.
What is the purpose of a condition expression in a loop?
Condition is an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running.
What does “iteration” mean in the context of loops?
Sometimes an algorithm needs to repeat certain steps until told to stop or until a particular condition has been met. Iteration is the process of repeating steps.
When does the condition expression of a while loop get evaluated?
The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
When does the condition expression of a while loop get evaluated?
The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.
When does the initialization expression of a for loop get evaluated?
The initialization step occurs one time only, BEFORE the loop begins. The condition is tested at the beginning of each iteration of the loop. If the condition is true ( non-zero ), then the body of the loop is executed next.
When does the condition expression of a for loop get evaluated?
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. If the expression evaluates to false, execution exits the loop and goes to the first statement after the for construct.
This conditional test is optional. If omitted, the condition always evaluates to true.
When does the final expression of a for loop get evaluated?
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition . Generally used to update or increment the counter variable. A statement that is executed as long as the condition evaluates to true.
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
What does the ++ increment operator do?
The increment operator (++) increments (adds one to) its operand and returns a value.
Array.prototype.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.
Array.prototype.pop()
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
Array.prototype.push()
The push() method adds one or more elements to the end of an array and returns the new length of the array.