JavaScript Flashcards
What is the purpose of variables?
To store data into
How do you declare a variable?
var variable name;
How do you initial (assign a value to) a variable?
=
What characters are allowed in variable names?
Letters, numbers, &, _
What does it mean to say that variable names are “case sensitive?”
Variables of the same name have to be typed with the same capitalization
What is the purpose of a string?
Store or manipulate text
What is the purpose of a number?
Store or manipulate numbers
What is the purpose of a boolean?
Data type showing binary states, true/false
What does the = operator mean in JavaScript?
Assigning a value to a variable
How do you update the value of a variable?
Reassign variable to a new value
What is the difference between null and undefined?
Null: non-existent or invalid object, null cannot be created unorganically, purposeful emptiness
Undefined: assigned to a variable with nothing
Why is it a good habit to include “labels” when you log values to the browser?
More clear which variables are being logged and in what order
Give five examples of JavaScript primitives
String, number, bigint, boolean, undefined, symbol, null
What data type is returned by an arithmetic operation?
Number
What is string concatenation?
The joining of two or more strings to create on string
What purpose(s) does the + plus operator serve in JavaScript?
Adding one value to another
What data type is returned by comparing two values (, ===, etc)?
Boolean
What does the += “plus-equals” operator do?
Add the value of the right operand to a variable and assigns the result to a variable
What are objects used for?
Groups a set of variables and functions
What are object properties?
Variables within a certain boundary in a storage location
Describe object literal notation
{ property: value, }
How do you remove a property from an object?
Delete
What are the two ways to get or update the value of a property?
Dot notation, bracket notation
What are arrays used for?
Stores a list of values
Describe array literal notation
[ variable1, variable 2, variable3]
How are arrays different from “plain” objects?
Instead of properties, arrays have index number for values
What number represents the first index of an array?
0
What is the length property of an array?
Total number of values
How do you calculate the last index of an array?
Create new variable (last index), assign to the result of length of array (number of students) minus 1. Create new variable (last student), assign to original array at the index of the new variable (last index). var lastIndex = numberOfStudents - 1; var lastStudent = students[lastIndex];
What is a function in JavaScript?
Reusable block of code
Describe the parts of a functiondefinition.
Name of a function, parameters code block
Describe the parts of a functioncall.
Name of the function, parentheses, argument
When comparing them side-by-side, what are the differences between a functioncalland a functiondefinition?
Function has keyword, code block and parameter
Call has a name and arguments
What is the difference between aparameterand anargument?
Parameter- placeholder
Argument- variable
Why are functionparametersuseful?
Act as a placeholder
What two effects does areturnstatement have on the behavior of a function?
- 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.
Why do we log things to the console?
Debugging tool
What is a method?
Functions being called of an object
How is a method different from any other function?
Method is associated with an object
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?
No, console.log
Is the return value of a function or method useful in every situation?
No
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Give 6 examples of comparison operators.
greater than, less than, ==, ===, !=, !==, <=, >=
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of anifstatement?
Having your code make a decision
Iselserequired in order to use anifstatement?
No
Describe the syntax (structure) of anifstatement.
If (condition) {
execute
}
What are the three logical operators?
&&, | |, !
How do you compare two different expressions in the same condition?
Logical operators
What is the purpose of a loop?
To repeat a portion of code a set amount of times
What is the purpose of aconditionexpression in a loop?
An expression that is tested every time a code loops
What does “iteration” mean in the context of loops?
One repetition of a code block
Whendoes theconditionexpression of awhileloop get evaluated?
Before the code block runs
Whendoes theinitializationexpression of aforloop get evaluated?
Before anything
Whendoes theconditionexpression of aforloop get evaluated
Before each iteration
Whendoes thefinalexpression of aforloop get evaluated?
After each iteration
Besides areturnstatement, which exits its entire function block, which keyword exits a loop before itsconditionexpression evaluates tofalse?
Break
What does the++increment operator do?
Increment by 1
How do you iterate through the keys of an object
Use a for-in loop
What are the four components of “the Cascade”.
Source order, inheritance, specificity & !important
What does the term “source order” mean with respect to CSS?
The styling provided for an element last in your stylesheet is the styling that will ultimately take effect
How is it possible for the styles of an element to be applied to its children as well without an additional CSS rule
Inheritance
List the three selector types in order of increasing specificity.
Type, class, id
Why is using!importantconsidered bad practice?
It makes debugging more difficult by breaking the natural cascading in stylesheets
Why do we log things to the console?
Debugging tool
What is a “model”?
A representation of something
Which “document” is being referred to in the phrase Document Object Model?
The whole html file
What is the word “object” referring to in the phrase Document Object Model?
The datatype object
What is a DOM Tree?
Element plus all of its children
Give two examples ofdocumentmethods that retrieve a single element from the DOM.
queryselector, getelementbyname
Give one example of adocumentmethod that retrieves multiple elements from the DOM at once.
queryselectorall
Why might you want to assign the return value of a DOM query to a variable?
Ease of logging
Whatconsolemethod allows you to inspect the properties of a DOM element object?
console.dir
Why would atag need to be placed at the bottom of the HTML content instead of at the top?
Because the html isn’t rendered yet
What doesdocument.querySelector()take as its argument and what does it return?
String, first element in the document with its name
What doesdocument.querySelectorAll()take as its argument and what does it return?
Object, node list
What is the purpose of events and event handling?
To show the action resulting from a user, the code executed once the event takes place
Are all possible parameters required to use a JavaScript method or function?
No
What method of element objects lets you set up a function to be called when a specific type of event occurs?
addEventListener
What is a callback function
Function passed into another function as an argument
What object is passed into an event listener callback when the event fires?
Event
What is theevent.target? If you weren’t sure, how would you check? Where could you get more information about it?
Where the event occurred in the DOM, console.log it, MDN
What is theclassNameproperty of element objects?
Gets and sets the value of the class attribute of the element
How do you update the CSS class attribute of an element using JavaScript
Query select element and assign to class name property
What is thetextContentproperty of element objects
The text content of its’ elements
How do you update the text within an element using JavaScript?
Query select the element and assign to text content property
Is theeventparameter of an event listener callback always useful?
No
Why is storing information about a program in variables better than only storing it in the DOM?
Variables can be used again anywhere in the JavaScript file
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 aform?
Submit
What does theevent.preventDefault()method do?
Prevents usual behavior of that object
What does submitting a form withoutevent.preventDefault()do?
Refreshes the page
What property of a form element object contains all of the form’s controls.
Element
What property of a form control object gets and sets its value?
Value
What is one risk of writing a lot of code without checking to see if it works so far?
Harder to debug
What is an advantage of having your console open when writing a JavaScript program?
Debugging
Does thedocument.createElement()method insert a new element into the page?
No
How do you add an element as a child to another element?
appendChild
What do you pass as the arguments to theelement.setAttribute()method?
Name, value
What steps do you need to take in order to insert a new element into the page?
Create new element, setAttributes, appendChild
What is thetextContentproperty of an element object for?
To change/insert text content of an element
Name two ways to set theclassattribute of a DOM element.
setAttribute, className
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
reusing it, name recognition
What is theevent.target?
The element to which the event is dispatched
Why is it possible to listen for events on one element that actually happen its descendent elements?
Event capturing
What DOM element property tells you what type of element it is
event.target.tagName
What does theelement.closest()method take as its argument and what does it return?
Closest ancestor element
How can you remove an element from the DOM
Remove
If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?
Add event listener to direct ancestor of element
What is the affect of setting an element todisplay: none
Hiding the element
What does theelement.matches()method take as an argument and what does it return?
Takes in a selector string, returns a boolean
How can you retrieve the value of an element’s attribute?
getAttribute
At what steps of the solution would it be helpful to log things to the console?
Checking variables in the loop
If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?
Adding an event listener to every single object
If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?
Separate conditional blocks for each view
What is JSON?
JavaScript Object Notation
What are serialization and deserialization?
Turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network
Turning a stream of bytes into an object in memory
Why are serialization and deserialization useful?
Preserving the data
Using the 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 to you store data inlocalStorage?
localStorage.setItem
How to you retrieve data fromlocalStorage?
localStorage.getItem
What data type canlocalStoragesave in the browser?
String
When does the’beforeunload’event fire on thewindowobject?
Before the page unloads
What is amethod?
A function which is a property of an object
How can you tell the difference between a methoddefinitionand a methodcall?
Method definition is the reusable code to be executed
Method call is when the code is executed
Describe methoddefinitionsyntax (structure).
{ methodproperty: value},
Describe methodcallsyntax (structure).
Object.method(argument);
How is a method different from any other function?
A method is called of an object
What is thedefining characteristicof Object-Oriented Programming?
Having objects that can contain both data (as properties) and behavior (as methods)
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
Working with complex things in simple ways
What does API stand for?
Application programming interface
What is the purpose of an API?
Offer a service to other pieces of software
What is’this’in JavaScript?
An implicit parameter
What does it mean to say that’this’is an “implicit parameter”?
Available in function’s code block even though it was never included in function’s parameter list or never declared
Whenis the value ofthisdetermined in a function;call timeordefinition time
Call time
How can you tell what the value of’this’will be for a particular function or methoddefinition?
‘this’ is nothing
How can you tell what the value of’this’is for a particular function or methodcall?
Object to the left of the dot, if no dot, it’s window
What kind of inheritance does the JavaScript programming language use?
Prototype-based inheritance
What is a prototype in JavaScript?
An object that contains properties and 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 strings, arrays, and numbers
They inherit it from a prototype object
If an object does not have its’ own property or method by a given key, where does JavaScript look for it?
In the prototypes
What does thenewoperator do?
Creates a blank object, adds a __proto__ property to the new object, gives ‘this’ variable a value, return ‘this’
What property of JavaScript functions can store shared behavior for instances created withnew
Prototype
What does theinstanceofoperator do?
Checks if value on right is present in value on left
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 thedocument, what is one way to delay the execution of a JavaScript function until some point in the future?
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 thedelayparameter fromsetTimeout()orsetInterval()?
0
What dosetTimeout()andsetInterval()return?
Positive integer value
What is a client?
Piece of computer software/hardware that accesses a service by available by a server
What is a server?
Piece of computer software/hardware that provides functionality for other programs or devices
Which HTTP method does a browser issue to a web server when you visit a URL?
Get
What three things are on the start-line of an HTTPrequestmessage?
HTTP method, request target/URL, HTTP version
What three things are on the start-line of an HTTPresponsemessage
Protocol version, status code, status text
What are HTTP headers?
Metadata of an http message
Where would you go if you wanted to learn more about a specific HTTP Header?
MDN
Is a body required for a valid HTTP request or response message?
No
What is AJAX?
Allows you to request data from a server and load it without having to refresh the entire page
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 byXMLHttpRequestobjects when they are finished loading the data from the server
Load
AnXMLHttpRequestobject has anaddEventListener()method just like DOM elements. How is it possible that they both share this functionality?
They both share the same event.target.prototype
What is a code block? What are some examples of a code block?
A group of statements delimited by a pair of curly braces
Function code blocks
What does block scope mean?
When a variable is defined within a block, not accessible outside a block
What is the scope of a variable declared withconstorlet?
Block scope
What is the difference betweenletandconst?
Let can be reassigned
Const cannot be reassigned
Why is it possible to.push()a new value into aconstvariable that points to anArray?
When you add to an array, you’re not reassigning the value of an array, you’re adding to the list of the array
How should you decide on which type of declaration to use?
If you want a value to be immutable, use const
If you want a value to be able to be reassigned, use let
What is the syntax for writing a template literal?
A string enclosed by back ticks
What is “string interpolation”?
The ability to substitute a part of the string for values of variables or expressions
What is destructuring, conceptually?
Breaking down an object, assigning the values of the object/array to variables
What is the syntax forObjectdestructuring?
let { property1: variable1, property2: variable2 } = object;
What is the syntax forArraydestructuring
let [x, y, z] = array;
How can you tell the difference between destructuring and creatingObject/Arrayliterals
Destructuring has curly braces on left side of =
Creating has curly braces on right side of =
What is the syntax for defining an arrow function?
Const fnName = () => {}
If there’s only one parameter, you don’t need parentheses
Zero parameters, or two or more parameters, parentheses are mandatory
When an arrow function’s body is left without curly braces, what changes in its functionality?
Automatically returned
How is the value ofthisdetermined within an arrow function?
Determined at definition time
What is a CLI?
Command line interfaces, commands to a computer program in the form of lines of text
What is a GUI?
Graphical user interface, form of user interface that allows users to interact with electronic devices through graphical icons and audio indicators instead of text-based user interfaces
Command ‘man’
Interface to the online reference manuals
Command ‘cat’
Concatenate files and print the output
Command ‘ls’
List information about files
Command ‘pwd’
Print name of current/working directory
Command ‘echo’
Display line of text
Command ‘touch’
Update the access and modification times of each file to current time
Command ‘mkdir’
Create directories that don’t already exist
Command ‘mv’
Move or rename files
Command ‘rm’
Remove files or directories
Command ‘cp’
Copy files and directories
What are the three virtues of a great programmer?
Laziness, impatience, hubris
What is Node.js?
JavaScript runtime, executing JavaScript code not in a browser
What can Node.js be used for?
Used for back-end services and to build scalable network applications
What is a REPL?
Read-Eval-Print-Loop, program running a programming language awaiting additional inputs
When was Node.js created
2009
What back end languages have you heard of?
Python, php, ruby, java, c++, C#, rust, golang, perl, visual basic, elixir, haskell, rust, Clojure, kotlin, wasm
What is theprocessobject in a Node.js program?
Global variable that provides information about, and control over, the current Node.js process
How do you access theprocessobject in a Node.js program?
Open node.js or explicitly access using require(‘process’)
What is the data type ofprocess.argvin Node.js?
Array
What is a JavaScript module?
Functionality organized in javascript files which can be reused throughout the node
What values are passed into a Node.js module’s local scope?
module, exports, require, __filename, __dirname
Give two examples oftrulyglobal variables in a Node.js program.
Global, process, setTimeout, setInternal
What is the purpose ofmodule.exportsin a Node.js module?
To export code from a given file to another file that can access its’ code
How do you import functionality into a Node.js module from another Node.js module?
Require.(‘./relative-path’)
What is the JavaScript Event Loop?
Runtime model that executes code, collects and processes events, executes queued sub-tasks
What is different between “blocking” and “non-blocking” with respect to how code is executed
Blocking runs code in sequence, non-blocking runs parallel
What is a directory
A folder that has a collection of files
What is a relative file path?
Location of file or folder relative to the working directory
What is an absolute file path?
Complete location of file or folder relative to the root directory
What module does Node.js include for manipulating the file system?
fs module
What method is available in the Node.jsfsmodule for writing data to a file?
writeFile()
Are file operations using thefsmodule synchronous or asynchronous?
Yes