JS Flashcards
What is the purpose of variables?
to store and remember information
How do you declare a variable?
the js keyword var var fullName = 'aly'
How do you initialize (assign a value to) a variable?
= assignment operator
What characters are allowed in variable names?
letters, $ and _
What does it mean to say that variable names are “case sensitive”?
captialization and lower case letters are different from each other
What is the purpose of a string?
to hold sentences and strings and a string of characters
What is the purpose of a number?
to hold the value of a number, and for using math in js
What is the purpose of a boolean?
T/F, its how js makes decisions in code
What does the = operator mean in JavaScript?
assignment operator, assigns a value to a variable
How do you update the value of a variable?
you change the value and whats on the right side of the assignment operator
What is the difference between null and undefined?
null represents and points to a nonexsitent or invalid object, a human purposely assigned its value to null.
undefined automatically assigned to variables that have just been declared. Never been assigned by a human that is js machine telling you something is undefined.
Why is it a good habit to include “labels” when you log values to the browser console?
a way of debugging and organization so you know what value goes to what variable
Give five examples of JavaScript primitives.
null, undefined, string, boolean, number
What data type is returned by an arithmetic operation?
number
What is string concatenation?
Concatenate is a fancy programming word that means “join together”. Joining together strings in JavaScript uses the plus (+)
let one = 'Hello, '; let two = 'how are you?'; let joined = one + two; joined;
What purpose(s) does the + plus operator serve in JavaScript?
adds one value to another
What data type is returned by comparing two values (, ===, etc)?
a boolean T/F
What does the += “plus-equals” operator do?
The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.
let a = 2; let b = 'hello';
console.log(a += 3); // addition // expected output: 5
console.log(b += ' world'); // concatenation // expected output: "hello world"
What are objects used for?
Objects group together a set of variables and functions to create a model
of a something you would recognize from the real world. In an object,
variables and functions take on new names.
key:value pair
What are object properties?
variables that hold information about the object, key:value
Describe object literal notation
how to access an object’s properties, or add/delete key value points.
pet.name = ‘scout’
How do you remove a property from an object?
using the delete keyword and bracket/dot notation
delete pet.name;
delete pet[‘name’];
What are the two ways to get or update the value of a property?
dot notation
pet.name
brackets - use if property name is a number or a variable is being used in place of the property name
What are arrays used for?
to list together a group of related data
Describe array literal notation.
variable with the name of the array, assign it to brackets and place each data type item inside with a comma.
var arr = [‘aly’, ‘sam’, ‘will’]
How are arrays different from “plain” objects?
They relate and hold together related data, objects hold a bunch of data that is about an object. Arrays are more specific in the items they hold, whereas objects can hold a bunch of properties that relate to the object itsself not the actual data pertaining to that property
What number represents the first index of an array?
0
What is the length property of an array?
tells you how many items are in that array
How do you calculate the last index of an array?
number of items in an array - 1
array.length - 1
What is a function in JavaScript?
code blocks that act like machines and run and transform data.
packing up code for reuse throughout a program
giving a name to a handful of code statements to make it code easier to read
making code “dynamic”, meaning that it can be written once to handle many (or even infinite!) situations
Describe the parts of a function definition.
The function keyword to begin the creation of a new function.
An optional name. (Our function’s name is sayHello.)
A comma-separated list of zero or more parameters, surrounded by () parentheses. (Our sayHello function doesn’t have any parameters.)
The start of the function’s code block, as indicated by an { opening curly brace.
An optional return statement. (Our sayHello function doesn’t have a return statement.)
The end of the function’s code block, as indicated by a } closing curly brace.
Describe the parts of a function call.
write the name of the function and then () to call it and pass in any arguments
getData(‘proton’)
When comparing them side-by-side, what are the differences between a function call and a function definition?
function definition is the name of the function and has the code inside that will actually do something with data. A call is when the function is already defined and its not time to run the function, just state the name of the function followed by ()
What is the difference between a parameter and an argument?
Parameters are within the function definition and acts as a placeholder for the incoming data when the function is called. when you are calling the function and actually pass in data, that is an argument
Why are function parameters useful?
Because they act as placeholders, we dont need the exact incoming data because we dont know what that will be, so parameters hold that spot in abtraction
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?
debugging to see what and where your problem is, also to explore and play around in JS
What is a method?
a function that is a property and is placed on a function to alter the object in some form
How is a method different from any other function?
you dont see the full function defintion and its code, also with methods you just place it onto the object itself using dot notation. objects are attached to an object.
How do you remove the last element from an array?
array.pop()
How do you round a number down to the nearest integer?
.floor()
How do you generate a random number?
Math.random()
How do you delete an element from an array
.shift() const array1 = [1, 2, 3];
const firstElement = array1.shift();
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1
How do you append an element to an array?
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
const months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// inserts at index 1
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “June”]
months.splice(4, 1, ‘May’);
// replaces 1 element at index 4
console.log(months);
// expected output: Array [“Jan”, “Feb”, “March”, “April”, “May”]
How do you break a string up into an array?
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' '); console.log(words[3]); // expected output: "fox"
Do string methods change the original string? How would you check if you weren’t sure?
strings do not change their original forms, if you are not sure just console.log
Roughly how many string methods are there according to the MDN Web docs?
A LOT around 25
Roughly how many array methods are there according to the MDN Web docs?
A LOT