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