Javascript Flashcards
What does = operator do in Javascript?
It is the assignment operator typically used for variables
Which keyword is used to declare a variable in Javascript?
var
Which characters is a Javascript variable allowed to begin with?
- Uppercase letters
- lowercase letters
- underscore
- dollar sign
- numbers (cannot use numbers in the beginning of the variable however)
What are quotation marks used for in Javascript?
Used for string variables
What is the purpose of strings in Javascript?
Storing and manipulating text
What is the purpose of booleans in Javascript?
To find out if an expression is true or false
What is the purpose of numbers in Javascript?
Sets up a basis of counting and quantifies any math related value
What does null mean in JavaScript?
Intentional absence in value
What data types can object properties hold?
They can basically hold any variable like strings, numbers, booleans, etc
Describe the syntax (structure) of object literals in JavaScript?
var variableName = { key: 'value1' key2: 'value2' ];
What is the principal use of arrays?
Organized lists for variables that can be called upon
Describe the syntax (structure) of array-literals in JavaScript
var variableName = [value1, value2, value3];
What number represents the first index of an array?
0
How can you access the last element of an array?
(Length property of an array) subtract by 1
What are the five parts of a function definition?
function name(parameters) { code } - The function keyword, the name for a function (optional), the parameters, the code block, and the return statement(optional) - Function names can have letters, digits, underscores, and dollar signs
How do you call a function?
- Using the function name with the parentheses and semicolon
Example: myFunction();
What is the difference between a parameter and an argument?
Parameter - listed inside the parentheses in a function. Behave as local variables
Argument - values received by the function when it is invoked
What does strictly equal mean?
=== It means that the values being compared are of the same value and data type
What is the logical <b>and</b> operator?
Both statements have to be true in order to result in true.
Can you name some comparison operators?
== equal to === equal value and equal type != not equal !== not equal or not equal type > greater than < less than >= greater than or equal to <= les than or equal to
When is the first expression in the parentheses for a for loop (known as the initialization) evaluated?
The first expression (initialization) is used once before the code block is executed.
Typically written as (let i = 0).
Sets up the loop
When is the second expression in the parentheses for a for loop (known as the condition) evaluated?
The second expression (condition) is used to define the condition for executing the code block.
This is typically written as (i < number). The number representing how many times the loop will occur.
Lets computer know when to stop looping.
When is the third expression in the parentheses for a for loop (known as the final expression) evaluated?
The third expression (the final expression) is used to change the value each time the code block in the loop has been executed.
Can be written as (i++)
Helps to get the loop closer to stopping.
What is the purpose of the condition in a for loop?
Dictates when the loop will stop running.
What is the purpose of the final expression in a for loop?
Gets the loop closer to stopping
What is DOM?
Document Object Model which is created when a web page is loaded.
Defines a standard for accessing documents
HTML DOM is a standard object tree model and programming interface for HTML.
Sets the standard for how to get, change, add, or delete HTML elements
What does document.querySelector() return?
Returns the first element within a document that matches a specified selector or group of selectors. If no matches then it returns null.
How do you modify the text of elements on the DOM?
var simplifiedElement = document.querySelector('id.idName'); simplifiedElement.textContent = 'insert text here';
What arguments does the addEventListener method make?
document.addEventListener(event, function, useCapture)
Event - required, the event name, do not use “on” as prefix
Function - required, the function to run when event occurs. When the event occurs the object is passed to the function as the first parameter.
useCapture - optional, a boolean. Specifies if the event should be executed in the capturing or bubbling phase
Give five examples of Javascript events that can be listened for
Mouse - events that relate to the computer mouse. Used to notify when mouse is clicked, doubleclicked, up/down events, right-click, movement in and out of an element, text selection, etc.
DOMContentLoaded - occurs on events related to events being loaded on the document
Animation - events related to Web Animation API. Used to respond to changes in animation (ex. when it starts/ends)
Composition - Events related to entering text “indirectly” (i.e. text entered via a speech to text engine or using special key combinations that modify keyboard presses to represent new characters in another language
Drag’n’drop, Wheel - Events derived from mouse events. While this is occurred when using the mouse wheel or drag/drop
What does document.createElement() take as its assignment?
It takes tagName or an HTMLUknownElement if tagName isn’t recognized.
For example (h1, div, etc)
What does document.createElement() return?
Returns the new element
How do you append elements to the DOM?
appendChild(aChild)
Example ($parent.appendChild($child)).
What is the purpose of variables?
To store values that can be used later
How do you initialize (assign a value to) a variable?
var varName = value
What does it mean to say that variable names are “case sensitive”?
Variables with different casing can have completely different values
What does = operator mean in JavaScript?
It means assignment as in it assigns a variable to a value
How do you update the value of a variable?
varName = new value
No need to declare value again using var because it has already been stored
What is the difference between null and undefined?
Null = it was intentionally left blank
Undefined = can be missing a value for any multiple of reasons
Why is it a good habit to include “labels’ when you log values to the browser console?
Provides clarity to other users and yourself in the future.
Give five examples of JavaScript data type primitives.
- number
- string
- boolean
- undefined
- null
What data type is returned by an arithmetic operation?
Number data type
What is string concatenation?
Combining string values together
Strings are immutable, cannot be changed
What purpose(s) does the + plus operator serve in JavaScript?
Adds or concatenates values together
What data type is returned by comparing two values (, ===, etc.)?
Boolean data type
What does the += “plus-equals” operator do?
Adds or concatenates a value on the right side to the left side
What are objects used for?
To group together a set of variables and functions
What are object properties?
Variables that are part of an object.
Individual keys that correlate to a value
Describe object literal notation?
Object literal notation contains curly braces ({ })
How do you remove a property from an object?
Using the delete operator and the object.property
Ex. delete object.property
What are two ways to get or update the value of property?
Using a . to call on the object property
Using [ ] to call on the object property
square bracket notation - contains a variable with values you actually want
dot notation - straight away that calls on an object’s property
What are arrays used for?
Stores a set of values/lists of data
Describe array literal notation.
[ ]
var arrayName = [value1, value2, value3];
How are arrays different from “plain” objects?
- They don’t need an individual key or value
- Arrays don’t have alpha-numeric indexes (only numeric)
- Arrays have a length property
What number represents the first index of an array?
[0]
What is the length property of an array?
Stores the value of the length of the array’s list via arrayName.length
How do you calculate the last index of an array?
arrayName - 1
Length is true count of value, the - 1 takes into account of indexing
What is the notation for calling an array in an object?
objectName.objectProperty.objectValue[#];
What is a function in JavaScript?
Sets up code that can be called upon any time in a program.
Describe the parts of a function definition.
- function keyword
- function name (optional)
- function parameters (optional)
- Code block {using curly braces}
- Return statement (optional)
Describe the parts of a function call.
- functionName
- (arguments, in, parentheses)
- Does not require arguments
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function Definition - function is defined with parameters in the parentheses
Function Call - function is called with arguments in the parentheses
What is the difference between a parameter and an argument?
Parameters are used when defining a function, placeholder for values
Arguments are used when calling a function, values are known
Why are function parameters useful?
Function parameters are useful because it allows other people to pass arguments when the function is called
Allows for reusability instead of single use (no parameters means one result)
What two effects does a return statement have on the behavior of a function?
The return statement produces a value and exits the function (no code after this return within the code block will run
Why do we log things to the console?
To make sure that we get the proper output from our code
What is a method?
A function which is a property of an object
i.e. Math.max(numberValues );
How is a method different from any other function?
Functions are objects while methods reference an object to a function
How do you remove the last element from an array?
arrayName.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?
object.splice();
How do you append an element to an array?
object.push();
How do you break a string into an array?
object.split();
Do string methods change the original string? How would you check if you weren’t sure?
No, strings are immutable.
Use the console.log and check the original string to see if it changed
Roughly how many string methods are there according to the MDN Web docs?
There are a lot… like 40-50
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
Is the return value of a function or a method useful in every situation?
Not always necessary
Roughly how many array methods are there according to the MDN Web docs?
There are a lot.. like 40-50
How to learn via documentation?
- Read the blurb that provides a definition/summary of the term/method you’re looking at.
- Syntax
- Read parameters
- Ignore info that doesn’t pertain to what you’re looking for
- Return Value
- Read original sample code
Give 6 examples of comparison operators
>, < >= <= == === != !==
What data type do comparison expressions evaluate to?
Boolean
What is the purpose of an if statement?
Create a path of code dependent on the user’s input/data
This is done by checking a condition. If the condition is true then the code block is executed.
Is else required in order to use an if statement?
No you can use just the if statement
Describe the syntax (structure) of an if statement?
keyword if
condition (yourConditionHere)
Code Block { }
Code to execute if true (inside the code block)
What are the three logical operators?
&&
||
!
How do you compare two different expressions in the same condition?
Using a logical operator
What is the purpose of a loop?
To run a piece of code a repeated number of times so long as the condition in the loop is true.
Once the condition of the loop is false then the code stops.
What is the purpose of a condition expression in a loop?
Sets the code up to repeatedly run so long as the conditions remain true and will stop the loop when the condition is false
What does “iteration” mean in the context of loops?
Each time the loop repeats