JavaScript Flashcards
What is the purpose of variables?
used to store data
How do you declare a variable?
using variable keyword
How do you initialize (assign a value to) a variable?
using variable keyword, variable name, assignment operator, and value
What characters are allowed in variable names?
can contain letters, digits, underscores, $
What does it mean to say that variable names are “case sensitive”?
This means that language keywords, variables, function names, and any other identifiers must always be typed with a consistent capitalization of letters (camelCase)
What is the purpose of a string?
datatype consists of
letters and other characters; store text data
What is the purpose of a number?
store numeric data
What is the purpose of a boolean?
used to create true/false statements.
What does the = operator mean in JavaScript?
assignment operator
How do you update the value of a variable?
reassignment; the var keyword would no longer be in the statement
What is the difference between null and undefined?
an undefined variable has been declared but not defined yet. null is an assigned value of nothing
Give five examples of JavaScript primitives.
number, strings, booleans, null, undefined
What data type is returned by an arithmetic operation?
number
What is string concatenation?
adding strings together
What purpose(s) does the + plus operator serve in JavaScript?
adds one value to another or concatenates strings
What data type is returned by comparing two values (, ===, etc)?
boolean
What does the += “plus-equals” operator do?
Addition assignment
What are objects used for?
group together related data to create a model
What are object properties?
unique named keys paired to values
Describe object literal notation.
made up of object name followed by key and value pairs wrapped in curly braces
How do you remove a property from an object?
delete object.property
What are the two ways to get or update the value of a property?
using dot notation or square brackets
What are arrays used for?
store a list of values
Describe array literal notation.
array name followed by the assignment operator then square brackets with values separated by commas
How are arrays different from “plain” objects?
arrays have order and methods for updating the array
What number represents the first index of an array?
0
What is the length property of an array?
results in the number of items in an array
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
block of code designed to perform a particular task
Describe the parts of a function definition.
begins with a function keyword, followed by a name (optional), then parameters in parentheses, then open curly brace to start code block, optional return statement, ends with closing curly brace
Describe the parts of a function call.
function name followed by parentheses
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition begins with the function keyword, contains curly braces, and an optional return statement. function call only has function name and arguments inside parentheses.
What is the difference between a parameter and an argument?
parameters serve as a placeholder in the function definition and arguments are passed when a function is called
Why are function parameters useful?
the parameter will be holding the value of the argument when the function’s code block is run
What two effects does a return statement have on the behavior of a function?
causes the function to produce a value and it exits the function code block
What is a method?
a function which is a property of an object
How is a method different from any other function?
methods are attached to objects
How do you remove the last element from an array?
.pop()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array?
.splice()
How do you append an element to an array?
.push()
How do you break a string up into an array?
.split()
Do string methods change the original string? How would you check if you weren’t sure?
strings are immutable and check with console.log() or documentation
Is the return value of a function or method useful in every situation?
no
Give 6 examples of comparison operators.
=== , > , < , !=, >=, <=
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
evaluate condition
Is else required in order to use an if statement?
no
Describe the syntax (structure) of an if statement.
begins with if followed by the condition in parentheses, then open curly brace for code block to be executed and closed with curly brace
What are the three logical operators?
&&, ||, !
How do you compare two different expressions in the same condition?
using logical operators
What is the purpose of a loop?
checks a condition and repeats a task if the value is truthy
What is the purpose of a condition expression in a loop?
gives us a stopping point
When does the condition expression of a while loop get evaluated?
evaluated before each iteration
When does the initialization expression of a for loop get evaluated?
evaluated once before the loop begins
When does the condition expression of a for loop get evaluated?
before each loop iteration/code block
When does the final expression of a for loop get evaluated?
after code block and before the condition
which keyword exits a loop before its condition expression evaluates to false?
break
What does the ++ increment operator do?
adds one to its operand and returns a value
How do you iterate through the keys of an object?
using for.. in loops
What event is fired when a user places their cursor in a form control?
‘focus’
What event is fired when a user’s cursor leaves a form control?
‘blur’
What event is fired as a user changes the value of a form control?
‘input’
What event is fired when a user clicks the “submit” button within a form?
‘submit’
What does the event.preventDefault() method do?
its default action should not be taken as it normally would be
What does submitting a form without event.preventDefault() do?
refreshes the page and information entered remains in the URL
What property of a form element object contains all of the form’s controls?
elements
What property of form a control object gets and sets its value?
value
What is an advantage of having your console open when writing a JavaScript program?
fix errors when they occur
What is JSON?
a standard text-based format for representing structured data based on JavaScript object syntax
What are serialization and deserialization?
serialization is converting a native object to a string; deserialization is converting a string to a native object
Why are serialization and deserialization useful?
network transmission;
deserialization helps store data as a string; serialization is used to access data
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify()
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse()
How do you store data in localStorage?
localStorage.setItem(‘key’, ‘value’)
How do you retrieve data from localStorage?
localStorage.getItem(‘keyname’)
What data type can localStorage save in the browser?
string data
When does the ‘beforeunload’ event fire on the window object?
when the window, the document and its resources are about to be unloaded
What is a method?
a function which is a property of an object.
How can you tell the difference between a method definition and a method call?
method definition starts with the function keyword followed by curly braces and a coding block being assigned to property; method call is object.method() parentheses can have arguments;
Describe method definition syntax (structure)
key name : function (parameters) followed by curly braces and code block
Describe method call syntax (structure)
object.method(arguments)
How is a method different from any other function?
method is a property of an object
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods)
What is “abstraction”?
a way of creating a simple model of important properties;being able to work with (possibly) complex things in simple ways
What does API stand for?
application programming interface
What is the purpose of an API?
simplifies programming by abstracting the underlying implementation and only exposing objects or actions the developer needs
What is this in JavaScript?
keyword refers to the object it currently belongs to
What does it mean to say that this is an “implicit parameter”?
it is available in a function’s code block even though it was never included in the function’s parameter list or declared
When is the value of this determined in a function; call time or definition time?
call time
What does this refer to in the following code snippet? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
character object
what is the result of the following code snippet? Why var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
character.greet();
‘it’s a-me, Mario!’
what is the result of the following code snippet? Why? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
var hello = character.greet; hello();
‘it’s a-me, undefined!’
bc the greet method is being stored in the new variable. the this keyword does not have any object to relate to. there is no object left to the dot of hello
How can you tell what the value of this will be for a particular function or method definition?
you don’t know what the value of this will be until function is called
How can you tell what the value of this is for a particular function or method call?
“the object to the left of the dot” when the function is called (as a method),if there is no value to the left of the dot when the function is called, then by default, this will be the global window object,if you cannot see the function being called, then you do not know what the value of this will be.
What kind of inheritance does the JavaScript programming language use?
prototype based inheritance (prototypal)
What is a prototype in JavaScript?
an object that contains properties and (predominantly) methods that can be used by other objects
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
methods are borrowed from prototype objects
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
prototype object
What does the new operator do?
create an instance of a user-defined object type or of one of the built-in object types that has a constructor function
What property of JavaScript functions can store shared behavior for instances created with new?
function.prototype property
What does the instanceof operator do?
tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object
What is a “callback” function?
a function passed into another function as an argument
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setInterval() or setTimeout()?
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0
What do setTimeout() and setInterval() return?
interval ID
What is AJAX?
a programming practice of building complex, dynamic webpages using a technology known as XMLHttpRequest
What does the AJAX acronym stand for?
Asynchronous JavaScript And XML
Which object is built into the browser for making HTTP requests in JavaScript?
XMLHttpRequest()
What event is fired by XMLHttpRequest objects when they are finished loading the data from the server?
load
An XMLHttpRequest object has an addEventListener() method just like DOM elements. How is it possible that they both share this functionality?
prototypal inheritance