JavaScript Flashcards
What is the purpose of variables?
Variables store data
How do you declare a variable?
Use the keyword var to create a variable and give it a name. Statement ends with a semicolon.
EX: var firstName;
How do you initialize (assign a value to) a variable?
Use the var key word to initialize a new variable. Use the = assignment operator to give the variable a value.
EX: var firstName = ‘Rachel’
What characters are allowed in variable names?
letters, numbers, dollar sign ($), or underscore (_).
What does it mean to say that variable names are “case sensitive”?
Use of capital and lower cases are important in the variable name. score and Score would be 2 different variables, but it is bad practice to create two variables that have the same name using different cases.
What is the purpose of a string?
The strings data type consists of letters and other characters. Strings are useful when working with any kind of text.
What is the purpose of a number?
The numeric data type handles numbers.
For tasks that involve counting or calculating sums, you will use numbers 0-9.
What is the purpose of a boolean?
Boolean data types can have one of two values: true or false. Booleans are helpful when determining which part of a script should run.
what does the = operator mean in JavaScript?
the equals sign (=) is an assignment operator. It says that you are going to assign a value to the variable/ update the value given to a variable.
How do you update the value of a variable?
variableName = newValue
What is the difference between null and undefined?
Null means nothing, undefined means the value has not been set/assigned yet.
Why is it a good habit to include “labels” when you log values to the browser console?
A console log “label” is a short string that describes the variable or value being logged.
Give 5 examples of JavaScript primitives.
String, number, boolean, undefined, null.
What data type is returned by and arithmetic operation?
Number
What is string concatenation?
Joining of 2 or more strings by using + operator
what purpose(s) does the + operator serve in JavaScript?
Arithmetic addition, concatenating strings
what data type is returned by comparing two values ( < , > , = = = , etc)?
boolean - true or false
What does the += “plus-equals” operator do?
addition assignment
What are objects used for?
Objects are used to group together a set of variables and functions. Used to group related data together.
what are object properties?
Properties tell us about the object. Property is a key-value pair.
Describe object literal notation.
var objectName = {
property: value,
property: value,
};
How do you remove a property from an object?
Using the delete operator followed by objectName.propertyName OR objectName[“propertyName”]
What are the two ways to get or update the value of a property?
.notation OR
[“bracket notation”]. .notation is preferable for objects.
Bracket notation is used when the property name is not actually known (it’s in a variable or i needs o be calculated. Bracket notation is also required when the property key is not a valid identifier (variable name)
What are arrays used for?
To store a list or set of values that are related to each other in one variable.
Describe array literal notation.
var arrayName = [‘item 1’ , ‘item 2’ , ‘item 3’];
How are arrays different from ‘plain’ objects?
Items in an array are in a numbered list. Each item in the array is automatically given a number called and index. Objects can have named properties for the values but arrays are numbered.
What number represents the first index of an array?
0 - computers start counting from 0, 1, 2, 3 etc.
What is the length property of an array?
The length property holds the number of items in the array.
How do you calculate the last index of an array?
lastIndex = arrayName.length - 1
What is a function in Javascript?
A group of steps/actions that are stored.
Describe the parts of a function definition.
function keyword, optional function name, optional parameters, opening curly brace, block of code to be executed when function is called. within the code block should be a return statement - closing curly brace.
Describe the parts of a function call.
Name of the function followed by ( ) with any arguments (if any) passed included in the parenthesis. Ending with semicolon.
When comparing them side-by-side, what are the differences between a function call and a function definition?
Function definition contains curly braces indicating a block of code to be executed. Function definition will also include the function keyword where the call does not.
What is the difference between a parameter and an argument?
Parameters variables without a value, the arguments is the value for that parameter.
Why are function parameters useful?
Parameters give us the ability to add in variance (mutability). Result can now vary based on the information thats passed in.
What two effects does a return statement 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?
console.log is a debugging tool geared towards development To check the output of your code and see if any errors occur
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?
Methods are called on a variable/object. Syntax is the main difference.
How do you remove the last element from an array?
pop( ) method.
How do you round a number down to the nearest integer?
Math.floor ( ) - floor method of the Math object.
How do you generate a random number?
Math.random ( ) - random method of the Math object. Range is between 0-1 not inclusive of 1. Then you can multiply that decimal by the maximum or minimum value you need to get a the range you want.
How do you delete an element from an array?
splice( ) method.
How do you append an element to an array?
push ( ) method.
How do you break a string up into an array?
split ( ) method.
Do string methods change the original string? How would you check if you weren’t sure?
Strings are not mutable so the original string can’t be changed. You can check by looking at documentation @ MDN / you can try it yourself in the console to see the outcome.
Roughly how many string methods are there according to the MDN Web docs?
Roughly 50 different methods / check for them on MDN
Is the return value of a function or method useful in every situation?
Sometimes the return is not necessary - but a return is always there if you need it. If you need the return later you can save the return in a variable
Roughly how many array methods are there according to the MDN Web docs?
Roughly 40 different methods / check for them on MDN
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.
< , > , >= , <= , === , !==
What data type do comparison expressions evaluate to?
Boolean - true or false
What is the purpose of an if statement?
the if statement evaluates (or checks) a condition. if the condition evaluates to be truthy, any statements in the subsequent code block are executed.
Is else required in order to use an if statement?
An else statement is not required to use an if statement. Else is a fallback statement.
Describe the syntax (structure) of an if statement.
if (condition) { //code to be executed if condition is truthy}
What are the three logical operators?
& & ; - logical and, | | - logical or, ! - logical not
How do you compare two different expressions in the same condition?
wrap each individual expression in parenthesis and wrap the whole condition in a set of parenthesis.
EX. if ( (score1 + score2) > (highScore1 + highScore2) ) { //code to execute }
What is the purpose of a loop?
To execute/repeat a code block any number of times.
What is the purpose of a condition? (in reference to loops)
A condition specifies the amount of times a code block should execute. If the condition is truthy the code block will run. When the condition is false the loop ends.
What does “iteration” mean in the context of loops?
round - or pass through the loop.
When does the condition expression of a while loop get evaluated?
Before each pass through the loop.
When does the initialization expression of a for loop get evaluated?
Initialization expression is evaluated once before the loop begins.