JavaScript Flashcards
What is the purpose of variables?
To give us a location to store data and information to do something with it and use it again in the future.
How do you declare a variable?
Use the var keyword and then followed by the name of the variable. example:
var totalPets =
How do you initialize (assign a value to) a variable?
Using the assignment operator ( = )
What characters are allowed in variable names?
Letters, numbers, $ and underscore.
numbers are allowed as long as it is not the first character in the value. So “cat1” works but not “1cat.”
What does it mean to say that variable names are “case sensitive”?
Being “case sensitive” means that keywords should be exact.
What is the purpose of a string?
To represent text rather than numbers.
What is the purpose of numbers?
To count, measure, and calculate.
(example: you would store it as a string because you don’t do ‘math’ with your zip code. A zip code is an identifier. Other examples include address, telephone number, and SSN)
What is the purpose of a boolean?
To define whether an expression is true or false.
Booleans’ purpose is for computers for making a choice. This gives comp only two choices: “true” or “false.”
What does the = operator mean in JS?
A symbol used to perform operations on operands (values and variables).
How do you update the value of a variable?
State var name, assignment operator and then the new value.
What is the difference between null and undefined?
null is the intentional absence of a value so then you can go back and store a value. It indicates that it does exist but it is not assigned anything.
Undefined means it is also empty but the computer will assign it as it not having any value.
Why is it a good habit to include “labels” when you log values to the a browser console?
labels describe what you are inputting. It is mainly for organization.
Give 5 examples of JS primitives.
JS primitive types are data that is not an object and has no methods or properties.
1) string
2) booleans
3) numbers (also bigint- which are for really big numbers)
4)null
5) undefined
What data type is returned by an arithmetic operation?
Number data type
What type of data type is NaN?
NaN = not a number BUT
NaN is a number data type.
What is string concatenation?
Combines more than one string together. It’s like a secondary functionality for the + operator other than for math.
What purpose(s) does the + plus operator serve in JavaScript?
It can concatenate string and serves as an addition command, adding numbers together.
What data type is returned by comparing two values (, ===, etc)?
Boolean values.
What does the += “plus-equals” operator do?
Takes right operand value and adds it to current value (left operand) and then becomes the new value of the left operand.
What are objects used for?
Group together a set of properties to store data.
What is the reason to group data together to make objects?
Objects are a collection of variables to put them in a category indicating that they are similar.
What are object properties?
Individual pieces of data assigned as a variable that is associated with an object.
Describe Object Literal Notation.
It starts with a variable and it is defined by an array of key:value pairs separated by colons and then a comma after every key:value pairs except for the last pair of the array.
example:
var student = {
firstName: ‘Sharon’,
lastName: ‘Tieu’
};
How do you remove a property from an object?
use the Delete operator or use dot notation or bracket notation.
What are the two ways to get or update the value of a property?
Dot notation or Bracket notation.
What are arrays used for?
Arrays are typically lists of data especially when we do not know how much data will be in this list.
They are special type of objects that hold a set of related key/value pairs but the key for each value is its index.
Describe array literal notation.
- state variable
- assignment equal operator
-followed by [ ] - inside the [ ], you list the values
How are arrays different from “plain” objects?
Objects are individually named data stored inside of them whilst an Array is data with an specific order
Objects do not have an order whereas Arrays do have an order.
How do you calculate the last index of an array?
length property - 1
example:
student.length - 1;
What number represents the first index of an array?
0
What is the length property of an array?
The total number of values that exists in the array.
What is the importance of Data Modeling?
Data models are an organization of data.
What is a function in Javascript?
In Javascript, Functions are are expressions that establishes a relationship between an input and output.
To interact with a function in JS, you can “define” a function or “call” a function.
Example:
function sayHello() {
var greeting = ‘Hello, World!’;
console.log(greeting);
}
Describe the parts of a function definition.
function sayHello( ) {
var greeting = ‘Hello, World!’;
console.log(greeting);
}
- The function keyword - begins the creation of a new function
a. example: function - An optional name
a. example: (our function’s name is sayHello.) - A comma-separated list of zero or more parameters, surround by ( ) parentheses
a. example: (Our sayHello function does not have any parameters.) - The function’s code block, as indicated by an { opening curly brace
- An optional return statement
a. example: (Our sayHello function does not have a return statement.) - The end of the function’s code block
a. example: as indicated by a } closing curly brace
Explain the difference between “Defining” a function and “Calling” a function. How does calling a function work?
(Defining a function IS DIFFERENT from calling a function- defining a function does NOT cause the code’s code block to run.)
Once defined, a function becomes another kind of object, however, now it is special in that it can now be CALLED. A function has to be called in order for the code’s code block to run.
Example:
sayHello( ); // will log “Hello, World!” into the console.
var greeting = ‘Hello, World!’;
console.log(greeting);
//can now be executed again and again by name without having to copy-paste the code inside over and over again.
Describe the parts in calling a function. (Describe the function call syntax)
example(arg1, arg2, arg3);
- the function’s name.
(in this case, our function’s name is called example.) - A comma-separated list of zero or more arguments surrounded by ( ) parenthesis.
(Our sayHello function does not have any arguments.)
The only way to call a function is parenthesis.
When comparing them side-by-side, what are the differences between a function call and a function definition.
Function definition:
- include function keyword and a code block
Function call:
- will have parenthesis **
What is the difference between a parameter and an argument?
Parameter - variables that are defined when the function is declared. These are initialized to the values of the arguments supplied. They are local variables to the function that can store values.
Argument - the real values that are actually stored there when the function is called.
Why are function parameters useful?
Function parameters are useful, because you can pass in certain data to be able to run it again which saves you time from writing several different codes to serve the same function.
What two effects does a return statement have on the behavior of a function?
- The return statement will be replaced by whatever the function called.
- A return statement also will stop the function entirely.
What is a method?
A method is a function which is a property of an object. There are two kinds of methods:
1) Instance Methods: built-in tasks performed by an object instance
2) Static Methods: tasks that are called directly on an object constructor.
Why do we use log things to the console?
To see if our code is working and running. It’s meant for checking values while we are debugging.
How is a method different from other functions?
Methods are associated with an object, while a function is not.
a method is a form of a function- but they’re just stored in an object.
How do you remove the last element from an array?
pop( ) method removes (pops) the last element of an array.
How do you generate a random number?
Math.random( )
How do you round a number down to the nearest integer?
Math.floor( )
How do you append an element to an array? (append = add to the end)
push( ) method
How do you break a string up into an array?
split( ) method
Is the return value of a function or method useful in every situation?
No.
example: pop( )
The pop( ) method has a return value that is the removed element of an array. It is meant to get rid of something but on the console.log( ) it will show you what you have removed.
Roughly how many array methods are there according to the WDN Web docs?
there’s a lot :3
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
6 examples of comparison operators.
greater than >
less than <
strictly equal to === (makes sure values are the same if data type and value are equal)
!== strict not equal to
!= is not equal to
What data type do comparison expressions evaluate to?
Booleans (true or false)
Describe the syntax of an if statement.
define function with a parameter.
if statement, opening ( with condition ) { opening curly brace, then a return statement ; then semicolon, then } closing curly brace for the if statement.
function introduceWarnerBros(name) {
if (name === ‘yakko’) {
return ‘we are the warner brothers’;
} ;
What are the three logical operators?
and , or, and not
&& and: both needs to be true to be true
|| or : one condition to be met.
! not:
How can two different expressions in the same condition be compared?
With Logical Operators
What are truthy values? Give an example.
A value that is considered true when encountered in a Boolean context.
Example: the string of “space” so ‘ ‘ and an empty object, even empty arrays (because objects and arrays take up space and memory!)
What are falsy values?
A value that is considered false when encountered in a Boolean context.
Example: NaN
What is the purpose of an if statement?
It allows the computer to make a decisions based on the criterias that we set.
What is the purpose of a loop?
To run a set of code(s) over and over again.
What is the purpose of a condition expression in a loop?
They are the “brakes” or checkpoints for your loop
What does “iteration” mean in the context of loops?
“iteration” is the number of times your code block loop runs on a repetitive basis.
When does the condition expression of a While loop get evaluated?
The condition expression of a While loop gets evaluated before each iteration. It is a way to check to see if the code block can even run.
When does the initialization expression of a For loop get evaluated?
The initialization expression gets evaluated ONCE BEFORE the loop begins.
When does the condition expression of a loop take place?
Before each iteration takes place.
When does the final expression of a for loop get evaluated?
After each iteration takes place.
Besides a return statement, which exits its function block, which keyword exits a loop before its condition expression evaluates to false?
Break
What does the ++ increment operator do? ( the difference between a++ and ++a ).
- Substitutes the operator
- and increments the variable by one
How do you iterate through the keys of an object.
For - in loop
Why do we log things to the console?
To make sure everything is loading properly. Helps with debugging and to make sure the code is running.
What is a “model”?
A model is a representation or recreation of something.
Which “document” is being referred to in the phrase Document Object Model?
The HTML document.
What is the word “object” referring to in the phrase Document Object Model?
The word “object” refers to the data type object in javascript.
What is a DOM Tree?
Document Object Model is a model of the HTML document that is represented as javascript objects.
So The DOM Tree is the representative chunk of a page as javascript objects.