JavaScript Flashcards
What is the purpose of variables?
Gives us a way to call data later
How do you declare a variable?
var varName;
How do you initialize (assign a value to) a variable?
Set the variable equal to the desired value var varName = value;
What characters are allowed in variable names?
Any number, letter, _ , or $
But they must start with a letter, underscore (_), or $
What does it mean to say that variable names are “case sensitive”?
You must capitalize the variable’s spelling the same, in order to use it
var weTall is not the same as var wetall
What is the purpose of a string?
Stings are the form of data that allow you to enter a chain of text.
What is the purpose of a number?
Numbers are used for calculations and placement
What is the purpose of a boolean?
Booleans are true and false
They are used to decide whether an action should be taken or not
What does the = operator mean in JavaScript?
It assigns a value to a variable
The assignment operator
How do you update the value of a variable?
Set it equal to a different value, lower in the code var name = 'old name'; name = 'new name';
What is the difference between null and undefined?
Null means the items do not exist yet. Its data type is an object which was defined by a developer intentionally.
Undefined means the variable has not been given a value yet. Its data type undefined.
Why is it a good habit to include “labels” when you log values to the browser console?
You can know, in the console, what you are logging and make sure you are referring to the correct data.
Give five examples of JavaScript primitives.
Numbers String Boolean Null undefined
What data type is returned by an arithmetic operation?
numbers
arithmetic: involving numbers (numeric)
What is string concatenation?
Adding two or more strings to make one new, larger string
‘Sting’ + ‘ adding’ = ‘Strings adding’
What purpose(s) does the + plus operator serve in JavaScript?
Adding numbers together
Concatenating strings
What data type is returned by comparing two values (, ===, etc)?
Booleans
True or false
What does the += “plus-equals” operator do?
+=: addition assignment operator
Adds the item on the right, to the item on the left. THEN it replaced the item on the left with the new value
What are objects used for?
Give us a way to collect our data into smaller groups
What are object properties?
The items within an object. Keys & their values
Describe object literal notation.
var varName = {
property: value,
property: value
}
How do you remove a property from an object?
delete object.property;
What are the two ways to get or update the value of a property?
Dot notation: object.property = x
Bracket notation: object[‘property’] = x
What are arrays used for?
To make a series of data you can tie to a specific number
When you dont know how many pieces of that data you need
Groups of data that have the same type of data
Describe array literal notation.
var varName = [‘item’, ‘item’, ‘item’]
How are arrays different from “plain” objects?
The values are assigned to counted indexes, as opposed to random, unordered properties
What number represents the first index of an array?
0
Arrays start on 0 and count up
What is the length property of an array?
arrayName.length gives the total number of an array
How do you calculate the last index of an array?
Since the array indexing starts at 0, to get the last value in your array, you can subtract 1 from the array.length
var endItem = arrayName.length -1;
What is a function in JavaScript?
A faster way to access a chain of commands we need to use once or several times, throughout our program
Describe the parts of a function definition.
function sayHello(name) { var $fullGreeting = "Hello, " + name; return $fullGreeting; } var fullGreeting = sayHello("John"); console.log(fullGreeting); -------------------------------------------------- "Hello, John"
Describe the parts of a function call.
function: function keyword
functionName
(paramaters, parameters)
{….code….}: code block
When comparing them side-by-side, what are the differences between a function call and a function definition?
Definition: This would be to console the function without () at the end. The result would be the full view of the function.
Calling the function would be to run it and expect a result. This is done by adding () at the end of the function’s name.
What is the difference between a parameter and an argument?
Parameters go into the structure of the function. They act as placeholders for the data users will be inputting
Arguments are the actual data users input which, will be used in our function accordingly.
Why are function parameters useful?
Parameters are the placeholders for functions.
They will be replaced in the code block with the data users provide.
What two effects does a return statement have on the behavior of a function?
It sends the desired variable out of the scope of the function, which can then be used by another variable: function greet (name) { var greeting = "Hello, " + name; return myName; } var sayHi = greet('john'); console.log(sayHi);
It ends the code block from running any lines below it.
Why do we log things to the console?
We log things to the console to make sure the value of an object is as intended.
What is a method?
A method is a function stored as a property inside of an object.
How is a method different from any other function?
A method is a function saved onto an object
We call them by naming the object, adding a DOT, and then the name of the method
sting.method();
How do you remove the last element from an array?
array.push()
How do you round a number down to the nearest integer?
Math.floor()
How do you generate a random number?
Math.random();
The number is a decimal between 0 and 1
How do you delete an element from an array?
.pop() remove last item
.shift() removed first
.splice(startIndex, # to delete, items to insert)
How do you append an element to an array?
.push() add to last
.unshift() add to first
.splice(startIndex, # to delete, items to insert)
How do you break a string up into an array?
array.split(‘separate indicator’);
If you want to separate each word, the separate indicator would be (“ “)
Each letter (“”)
Do string methods change the original string? How would you check if you weren’t sure?
No, they make copies you can assign to new variables
Roughly how many string methods are there according to the MDN Web docs?
There are lots!
Around 40 or so
Is the return value of a function or method useful in every situation?
Sometimes, but not always needed
Roughly how many array methods are there according to the MDN Web docs?
There are lots!
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN