JavaScript Flashcards
What is the purpose of variables?
A script will have to temporarily store the bits of information it
needs to do its job. It can store this data in variables.
How do you declare a variable?
Before you can use a variable, you need to announce that you want to use it. This involves creating the variable and giving it a name. Programmers say that you declare the variable.
var quantity;
var = variable keyword
quantity = variable name
If a variable name is more than one word, it is usually written in camelCase. This means the first word is all lowercase and any subsequent words have their first letter capitalized.
How do you initialize (assign a value to) a variable?
Once you have created a variable, you can tell it what information you would like it to store for you. Programmers say that you assign a value to the variable.
quantity = 3;
quantity = variable name
3 = variable value
= is the assignment operator
Until you have assigned a value to a variable, programmers say the value is undefined.
What characters are allowed in variable names?
letters, numbers, dollar sign $, or an underscore _
numbers cannot be the first character of a variable name
What does it mean to say that variable names are “case sensitive”?
For the variable to be recognized, it needs to be entered with the same casing as it was when it was declared.
What is the purpose of a string?
Strings can be used when working with any kind of text. They are frequently used to add new content into a page and they can contain HTML markup.
Strings data type consists of letters and other characters. String data type is enclosed with a pair of quotes. These can be single or double quotes, but the opening quote must match the closing quote.
What is the purpose of a number?
The numeric data type handles numbers. Numbers are not only used for things like calculators; they are also used for tasks such as determining the size of the screen, moving the position of an element on a page, or setting the amount of time an element should take to fade in.
What is the purpose of a boolean?
Boolean data types can have one of two values: true or false. To determine if something is true or false, and to tell if script should run or not in conditionals.
You can think of it a little like a light switch - it is either on or off. As you will see in Chapter 4, Booleans are helpful when determining which part of a script should run.
What does the = operator mean in JavaScript?
It is the assignment operator. It assigns whatever is on the right side, to whatever is on the left side.
i.e. var quantity = 3
3 is being assigned to the variable quanitity
How do you update the value of a variable?
Reassigning it without declaring it again, you can leave off the var
i.e.
var totalPets = 1000;
totalPets = 1001;
What is the difference between null and undefined?
Null is intentional whereas undefined hasn’t been assigned yet
Why is it a good habit to include “labels” when you log values to the browser console?
If you do not include “labels”, it can be very confusing instead of helpful. A console log “label” is simply a short string that describes the variable or value being logged.
Since the console.log( ) method can write multiple values to the console at the same time, it’s easy to include a “label” when printing a value.
i.e.
console.log(‘typeof fullName’, typeof fullName) ->
typeof fullName: string
Give five examples of JavaScript primitives.
string, number, bigint, boolean, undefined, symbol, and null.
What data type is returned by an arithmetic operation?
Performs basic math, it returns a number
Addition operator is + (Adds one value to another)
Subtraction operator is - (Subtracts one value from another)
Division operator is / (Divides two values)
Multiplication operator is * (Multiplies two values using an asterisk)
Increment operator is ++ (Adds one to the current number)
Decrement operator is – (Subtracts one from the current number)
Modulus operator is % (Divides two values and returns the remainder)
Several arithmetic operations can be performed in one expression, but it is important to understand how the result will be calculated. Multiplication and division are performed before addition or subtraction. This can affect the number that you expect to see.
What is string concatenation?
There is just one string operator: the+ symbol. It is used to join the strings on either side of it.
There are many occasions where you may need to join two or more strings to create a single value. Programmers call the process of joining together two or more strings to create one new string concatenation.
For example, you might have a first and last name in two separate variables and want to join them to show a fullName. In this example, the variable called fullName would hold the string ‘Ivy Stone’.
var firstName = ‘Ivy ‘ ;
var lastName = ‘ Stone’ ;
var fullName = firstName + lastName;
What purpose(s) does the + plus operator serve in JavaScript?
Adding number values and concatenation.
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
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. Addition or concatenation is possible.
i.e.
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.
What are object properties?
If a variable is part of an object, it is called a property. Properties tell us about the object, such as the name of a hotel or the number of rooms it has. Each individual hotel might have a different name
and a different number of rooms.
Describe object literal notation.
Literal notation is the easiest and most popular way to create objects. The object is the curly braces and their contents. The object is stored in a variable called hotel, so you would refer to it as the hotel object.
var hotel = {
name: ‘Quay’,
rooms: 40,
booked: 25,
checkAvailability: function () {
return this.rooms - this.booked;
}
};
hotel = object
var = key
name: ‘Quay’, = property
rooms: 40, = property
booked: 25, = property
checkAvailability: function () { = method
return this.rooms - this.booked;
}
How do you remove a property from an object?
delete object.property
i.e. delete hotel.name;
If you just want to clear the value of a property, you could set it to a blank string.
hotel.name = ‘ ‘;
What are the two ways to get or update the value of a property?
dot notation and square bracket syntax
Bracket notation
Allows us to put a variable in the bracket and use the value stored in that variable, as the property name
i.e.
var prop = ‘color’;
vehicle[prop] = ‘white’;
Notice how there are no quotes around [prop], because it is not a string, but a variable.
It also allows us to create or access illegal names for variables/objects
Primitive data types vs. Reference data types
They are stored differently.
What are arrays used for?
An array is a special type of variable. It doesn’t just store one value; it stores a list of values.
You should consider using an array whenever you are working with a list or a set of values that are related to each other.
Arrays are especially helpful when you do not know how many items a list will contain because, when you create the array, you do not need to specify how many values it will hold.
Describe array literal notation.
The values in the array do not need to be the same data type, so you can store a string, a number and a Boolean all in the same array.
This technique for creating an array is known as an array literal. It is usually the preferred method for creating an array. You can also write each value on a separate line:
colors = [ ‘white’,
‘black’,
‘custom’];
How are arrays different from “plain” objects?
The key for each value in an array is an index number. The index number is the location in the array.
What number represents the first index of an array?
0
What is the length property of an array?
The length property tells you how many items are in the array.
How do you calculate the last index of an array?
array.length - 1
What is a function in JavaScript?
Functions allow you to package up code for use later in your program.
Describe the parts of a function definition.
Function definitions are made of:
- the function keyword
- an optional name
- zero or more parameters
- a code block
- an optional return statement
Describe the parts of a function call.
Functions are a special kind of object that is “callable”. Functions are called with ( ) parentheses and passed zero or more arguments.
The function name followed by the arguments replacing the parameters in ( )
When comparing them side-by-side, what are the differences between a function call and a function definition?
The call is just the name and arguments (within parentheses)
What is the difference between a parameter and an argument?
Parameters are placeholders for arguments. The key thing to remember about parameters and arguments is that when we define a function, we declare parameters and that when we call a function, we pass it arguments.
Why are function parameters useful?
They let us pass data into the function and to variables that are only locally accessible to them
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.
Are variables within a function accessible?
No, variables defined within a function are inaccessible from the outside. However, they can access variables on the outside of the function.
(You cannot view it from the outside, but you can look outside - like a car’s tinted window)
If they aren’t defined as a variable, then they can be accessed from the outside. They would be noted as an auto global.
Why do we log things to the console?
The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code. The console is a crucial learning tool and should be used as much as possible to play with and learn about JavaScript. You should be using console.log ( ) and checking the console output frequently, so be sure to include “labels” with your console.log ( )s.
The user does not see the console. Anything you print the console, the end user will not see it, unless you add it to the DOM somewhere.
What is a method?
A method is a function which is a property of an object. There are two kinds of methods: instance methods which are built-in tasks performed by an object instance, or static methods which are tasks that are called directly on an object constructor.
How is a method different from any other function?
A method is a property of an object (assigned to the property of an object)
a method can be turned into a function if it is assigned to a property of an object
How do you remove the last element from an array?
the pop method
How do you round a number down to the nearest integer?
Math.floor method. The floor method of the Math object
How do you generate a random number?
Math.random method.
The random method of the Math object (the range of numbers generated is 0-1)
How do you delete an element from an array?
The splice method. It needs start and delete count parameters to know index to delete and how many to delete from there.
library.splice(1, 1)
Start at 1, delete 1
How do you append an element to an array?
The push method - the quickest way to append an element to an array
How do you break a string up into an array?
The split method
var fullName = ‘Lisa Nguyen’
It will split Lisa and Nguyen
Do string methods change the original string? How would you check if you weren’t sure?
They don’t, they can create new strings stored to new variables, you can check by console logging them (the original variable, if the console log shows the original string, that means that the original string was not modified) and MDN. There is no way to change an existing string.
Avoid using string methods that have a trash icon because they are deprecated (on MDN website)
Roughly how many string methods are there according to the MDN Web docs?
50
Is the return value of a function or method useful in every situation?
No, you don’t always need a return value when calling methods (splice, push, unshift) because if you are using it to modify something, you don’t need to return it
Roughly how many array methods are there according to the MDN Web docs?
80+
What three-letter acronym should you always include in your Google search about a JavaScript method or CSS property?
MDN
How do you join a split string together?
Using the join method. Join can be used to remove white space.
What is JavaScript also known as?
ECMA Script. This is used my developers who create the engines that run JavaScript.
Give 6 examples of comparison operators.
< less than
> greater than
== is equal to
!= is not equal to
=== strictly equal to
!== strictly not equal to
>= greater than or equal to
<= less than or equal to
What data type do comparison expressions evaluate to?
boolean
What is the purpose of an if statement?
The if statement evaluates (or checks) a condition. If the condition evaluates to true, any statements in the subsequent code block are executed.
If the condition resolves to false, the statements in that code block are not run. (The script continues to run from the end of the next code block.)
Is else required in order to use an if statement?
It is not required to use an if statement.
The if ….else statement checks a condition. If it resolves to true, the first code block is executed. If the condition resolves to false, the second code block is run instead.
i.e.
if (score >= 50) {
congratulate();
}
else {
encourage();
}
If you require code to run only when the statement returns true (and do nothing else if false) then an else statement is not needed.
Describe the syntax (structure) of an if statement.
key word, condition, opening curly brace (start of code block), code to be executed, closing curly brace (end of code block)
if ( ) {
};
What are the three logical operators?
&& and
|| or
!= not
How do you compare two different expressions in the same condition?
using logical operators
If both of them need to be true, use &&
If only one of them need to be true, use or ||
What is the purpose of a loop?
Loops check a condition. If it returns true, a code block will run. Then the condition will be checked again and if it still returns true, the code block will run again. It repeats until the condition returns false. There are three common types of loops:
for, while, do while
What is the purpose of a condition expression in a loop?
The purpose is to end the loop. When the condition is no longer true, the loop ends. The script moves to the next line of code. Giving conditions for the code to be executed and where to stop.
What does “iteration” mean in the context of loops?
Each time the loop runs
When does the condition expression of a while loop get evaluated?
before each iteration
When does the initialization expression of a for loop get evaluated?
before anything else (the first time the for loop runs)
When does the condition expression of a for loop get evaluated?
before each loop iteration and after the initialization expression
When does the final expression of a for loop get evaluated?
the end of each loop iteration (after the work is completed)
Besides a return statement, which exits its entire function block, which keyword exits a loop before its condition expression evaluates to false?
a break statement will break out of the loop but not out of a function
What does the ++ increment operator do?
It is the incremental operator adding one to the counter, and assigns it to the i variable.
it substitute first and then adds after
i.e.
var a = 1
a++ will return 1
logging a will return 2
if you do ++a, it will increment the value first, and then substitute it
++a will return 3 and logging a will return 3
How do you iterate through the keys of an object?
With a For In Loop
Why do we log things to the console?
The JavaScript console is a debugging tool. It is where the browser prints errors and warnings as they occur in your JavaScript code. The console is a crucial learning tool and should be used as much as possible to play with and learn about JavaScript. You should be using console.log ( ) and checking the console output frequently, so be sure to include “labels” with your console.log ( )s.
The user does not see the console. Anything you print the console, the end user will not see it, unless you add it to the DOM somewhere.
What is a “model”?
When the browser loads a web page, it creates a model of the page in memory. The model is called a DOM tree, and it is stored in the browsers’ memory.
It is a representation of something.
Which “document” is being referred to in the phrase Document Object Model?
Document Object - The current web page (the HTML document) loaded into each window is modelled using a document object. The title property of the document object tells you what is between the opening < title > and closing
< / title > tag for that web page.