Javascript Flashcards
What is the purpose of variables?
To remember data
How do you declare a variable?
Use a variable keyword and write the variable name after it separated by a space
Ex. var isVariable;
How do you initialize (assign a value to) a variable?
You use an assignment operation like (=, <, >) to assign a value.
Ex. var isVariable = 10;
What characters are allowed in variable names?
Must begin with a letter. Dollar sign, or an underscore, numbers
CANNOT start with number
What does it mean to say that variable names are “case sensitive”
Variable names are remembered differently when the same letter is capitalized versus not.
Ex. isVariable /= isvariable
What is the purpose of a string?
To hold text within the quotation marks, which can include numbers, letters, symbols that the computer does not read as code, only characters.
What is the purpose of a number?
Remembers that it is a mathematical number, not text, used for math or incrementation
What is the purpose of a boolean?
To define whether it is true or false, basically made for decision making
What does the = operator mean in JavaScript?
Assignment operator to set a value to something
How do you update the value of a variable?
Add another value to the same variable on a different line
The key word var is not needed for the second time since the variable was already assigned.
Ex. isCool = 7 instead of var isCool = 7
What is the difference between null and undefined?
Null is an empty value but this has to be assigned and is not defined by the computer. This means the developer said this is empty on purpose. Considered typeof object because of a bug.
Undefined also is an empty value no value is assigned, but javascript is the one telling you there is no value.
Why is it a good habit to include “labels” when you log values to the browser console?
So that you know which value belongs to which variable.
Give five examples of JavaScript primitives.
Boolean, number, string, null, undefined
What data type is returned by an arithmetic operation?
Number
(if you do something wrong with the arithmetic operation, javascript will output NaN aka not a number)
What is string concatenation?
When you use the + sign between strings, the result is the 2 strings put together.
What purpose(s) does the + plus operator serve in JavaScript?
Can add like in math
Can also combine 2 strings together
What data type is returned by comparing two values (<, >, ===, etc)?
boolean
What does the += “plus-equals” operator do?
It adds the value of the right and then adds it to the variable on the left and outputs the sum of the total value
What are objects used for?
They’re used to store a lot more than just 1 value. They can store as many properties for 1 variable.
What are object properties?
They are basically variables inside objects. Variables can only hold 1 value, but objects can hold multiple properties of the variable to keep all the data organized without creating a gazillion variables.
Describe object literal notation.
They are the easiest and most popular way to create objects. You define the variable and then type the different properties and their values in curly braces.
Ex. var hotel {
name: ‘Quay’,
rooms: 40,
booked: 25
};
How do you remove a property from an object?
Write delete before the property
Ex. delete student.lastName
What are the two ways to get or update the value of a property?
Dot notation
var.newProperty
Bracket notation
var[‘newProperty’]
What are arrays used for?
Also used to store information, but in an orderly fashion
Describe array literal notation.
[ ]; Square brackets with values separated by commas
How are arrays different from “plain” objects?
They are numbered, so easier to organize and recall via their number
Adding items to arrays uses push operator whereas objects use the assignment operator
Arrays have length properties whereas objects do not.
What number represents the first index of an array?
0
This is why it’s how many spaces to move to get to the value. To get to the first value, you don’t have to move any space. To get to the second value, you move 1 space.
What is the length property of an array?
Calculates the number of properties in the array
Do not have to calculate on our own
How do you calculate the last index of an array?
The length of the array - 1
What is a function in JavaScript?
They allow you to package up code for use later in your program
It’s a list of steps of a process that can be repeated.
They are a special kind of object that is “callable” and reusable