JavaScript Flashcards
var fullName = 'Cody Miller'; var isCool = true; var totalPets = 1000;
on line 1 the string ‘Cody Miller’ is being assigned to the new variable fullName;
on line 2 the boolean true is being assigned to the new variable isCool;
on line 3 the number 1000 is being assigned to the new variable totalPets
var firstName = 'Ron'; var lastName = 'Swanson'; var fullName = firstName + ' ' + lastName;
on line 1, the string ‘Ron’ is being assign to the new variable firstName;
on line 2, the string ‘Swanson’ is being assign to the new variable lastName;
on line 3, the value of the variable firstName is being concatonated with the string ‘ ‘, the result of that expression is being concationeted with the value of the variable lastName, and the result of that expression is being assigned to tthe new variable fullName.
console. log(‘value of isAcidic:’, isAcidic);
console. log(‘typeof isAcidic:’, typeof isAcidic);
on line 1 we have the log method of the console object being called with two arguments: the string ‘value of isAcidic’ and the value of the isAcidic variable;
on line 2 we have the log method of the console object being called with two arguments: the string ‘typeof isAcidic’ and the result of the expression ‘typeof isAcidic’.
var pet = { name: 'Bilu', type: 'doggo' };
delete pet.name;
delete pet.type;
on line 1, we have an object literal being assigned to the new variable pet;
on line 2, we have the string ‘Bilu’ being assigned to the property name;
on line 3, we have the string ‘doggo’ being assigned to the property type;
on line 4, we have the closing curly braces for the object literal;
on line 6, we have the delete operator being used on the name property of the pet object;
on line 7, we have the delete operator being used on the type property of the pet object.
What is the purpose of variables?
To store data for later use
How do you declare a variable?
with the keyword var, const or let, followd by the name of the new variable
How do you initialize (assign a value to) a variable?
keyword (let, const, var) variableName = value;
What characters are allowed in variable names?
upper/lower case letters, $, _, and numbers when not in the first position
What does it mean to say that variable names are “case sensitive”?
that upper case letters and lower case letters are dealt as different characters, and so “cat” and “Cat” are dealt as different names.
What is the purpose of a string?
to store text data
What is the purpose of a number?
to be use in arithmetic operations, store numerical data for later use and manipulation, etc.
What is the purpose of a boolean?
to store true/false values, allowing for binary operations, branching decision making, etc.
What does the = operator mean in JavaScript?
the assignment of new value, from the right side to the left
How do you update the value of a variable?
variableName = newValue;
What is the difference between null and undefined?
Null is an empty value that does not appears naturally, and thus can only appear where intentionally put there by the programmers;
Undefined is an empty value that can appear as result of errors in the code, and thus is more useful if never assigned by the programmers;
Why is it a good habit to include “labels” when you log values to the browser console?
So to identify promptly the source of any error that shows up in your console.
Give five examples of JavaScript primitives.
number, string, boolean, undefined, null
What data type is returned by an arithmetic operation?
number
What is string concatenation?
the creation of a nem string by adding two or more strings at the tail end of the one at the left side of a + sign
What purpose(s) does the + plus operator serve in JavaScript?
addition and concatenation
What data type is returned by comparing two values (< , > , ===, etc)?
boolean
What does the += “plus-equals” operator do?
adds (or concatanates) the value of the variable to a new value, and then assigns the result of that operation to the original variable
What are objects used for?
storing variables (as properties) and functions (as methods) to create a relationship between them and the object, usually to better describe a real world structure
What are object properties?
variables stored inside objects.
Describe object literal notation.
var object = { property: value, method (arguments) = {
}
};
How do you remove a property from an object?
using the delete operator:
delete object.property;
What are the two ways to get or update the value of a property?
object.property or object[‘property’]
What are arrays used for?
to store a list of values, accecible by an index. It’s when the order or the quantity is the important factor, not the name of properties, that arrays come in hand
Describe array literal notation.
var array = [ , , , ];
How are arrays different from “plain” objects?
they have indexed values instead of property:value pairs
What number represents the first index of an array?
0
What is the length property of an array?
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?
a reusable block of code that can accept input and return a result, or execute an action and return no value.
Describe the parts of a function definition.
- function: keyword to declare a function;
- name (optional) how to call the function in the future: name();
- ( , , , ): a comma-separated list of zero or more parameters, surrounded by () parentheses;
- the start of the function’s code block, as indicated by an { opening curly brace;
- lines of code;
- return (optional): a statement to return a value;
- the end of the function’s code block, as indicated by a } closing curly brace.
function name( , , , ) { //lines of code //return }
Describe the parts of a function call.
- name(): the name of the function being called;
- ( , , , ): a comma-separated list of zero or more arguments, surrounded by () parentheses;
name( , , , );
When comparing them side-by-side, what are the differences between a function call and a function definition?
we have 1. function (keyword), 2. parameters (placeholder names) instead of arguments (values) and 3. code block between curly braces on function definitions in contrat with function calls.
What is the difference between a parameter and an argument?
a parameter is a placeholder in the function definition for the values to be passed as arguments in a function call.
Why are function parameters useful?
without them, functions can’t accept input (in the form of arguments) later on, so they’ll aways execute the same actions without variation.
What two effects does a return statement have on the behavior of a function?
it 1. exists the function and 2. returns a value from the function (to be stored, used, manipulated, etc.)
Why do we log things to the console?
to see and debug the code as we write it.
What is a method?
a function passed as a property of an object
How is a method different from any other function?
They’re not, though methods are related to their objects while functions are not
How do you remove the last element from an array?
array.pop()