Prework 01 Flashcards

(28 cards)

1
Q

The ______ method adds an item to the end of an array.

A

push()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The ______ method removes an item to the end of an array.

A

pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The ______ method adds the first element of an array. It also returns the element added.

A

unshift()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The ______ method adds the first element of an array. It also returns the element added.

A

unshift()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The ______ method removes the first element of an array. It also returns the element removed.

A

shift()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In order for JavaScript to compare two different data types (for example, numbers and strings), it must convert one type to another. This is known as ______ ______.

A

Type Coercion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
“ = “
vs 
“ == ” 
Vs
“ === “
vs
“ != “
vs
“ !== “
A

“ = “ is for assignment. Assigns a value to the left operand depends on the value of operand available on the right side. The first operand should be a variable.

“ == ” is the equality operator. it checks whether its two operands are the same or not by changing expression from one data type to others (This conversion is known as “Type Coercion”).

“ === “ (triple equals) is a strict equality comparison operator. returns 0 for the values which are not of a similar type. This operator performs type casting for equality. Does not perform a type conversion. If we compare 2 with “2” using ===, then it will return a 0 value.

“ != “ The inequality operator (!=) is the opposite of the equality operator. It means not equal and returns false where equality would return true and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing.

“ !== “ The strict inequality operator is the logical opposite of the strict equality operator. It means “Strictly Not Equal” and returns false where strict equality would return true and vice versa. The strict inequality operator will not convert data types. Not “ !=== “.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Comparison operators in JavaScript return a ______ ______ or ______ value.

A

All of these operators return a boolean true or false value.

The most basic operator is the equality operator ==. The equality operator compares two values and returns true if they’re equivalent or false if they are not.

Note that equality is different from assignment (=), which assigns the value on the right of the operator to a variable on the left.

In order for JavaScript to compare two different data types (for example, numbers and strings), it must convert one type to another. This is known as Type Coercion. Once it does, however, it can compare terms as follows:

1 == 1
1 == 2
1 == ‘1’
“3” == 3

In order, these expressions would evaluate to true, false, true, and true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In a Switch Statement, Case values are tested with ______ _______.

A

In a switch statement, case values are tested with strict equality (===).

A break in a switch statement tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Comparison operators return a boolean true or false value.

Sometimes people use an if/else statement to do a comparison, like this:

function isEqual(a,b) {
  if (a === b) {
    return true;
  } else {
    return false;
  }
}

What’s the better way to do this?

A

Since === returns true or false, we can return the result of the comparison:

function isEqual(a,b) {
  return a === b;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Return Early Pattern for Functions

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

What will happen in the example below?

function myFun() {
  console.log("Hello");
  return "World";
  console.log("byebye")
}
myFun();
A
function myFun() {
  console.log("Hello");
  return "World";
  console.log("byebye")
}
myFun();

The above will display the string Hello in the console, and return the string World. The string byebye will never display in the console, because the function exits at the return statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Uninitialized Variables

When JavaScript variables are declared, they have an initial value of ______.

A

Uninitialized Variables

When JavaScript variables are declared, they have an initial value of undefined.

If you do a mathematical operation on an undefined variable your result will be NaN which means “Not a Number”. If you concatenate a string with an undefined variable, you will get a literal string of undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In JavaScript, an ______ is a standalone entity, with properties and type.

A

In JavaScript, an object is a standalone entity, with properties and type.

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.

Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.

Here’s a sample cat object:

var cat = {
  "name": "Whiskers",
  "legs": 4,
  "tails": 1,
  "enemies": ["Water", "Dogs"]
};

In this example, all the properties are stored as strings, such as - name, legs, and tails. However, you can also use numbers as properties. You can even omit the quotes for single-word string properties, as follows:

var anotherObject = {
  make: "Ford",
  5: "five",
  "model": "focus"
};

However, if your object has any non-string properties, JavaScript will automatically typecast them as strings.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If the property of the object you are trying to access has a space in its name, you will need to use ______ notation.

A

Besides dot notation ( [.] ), another way to access the properties of an object is bracket notation ( [] ). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.

However, you can still use bracket notation on object properties without spaces.

Here is a sample of using bracket notation to read an object’s property:

var myObj = {
  "Space Name": "Kirk",
  "More Space": "Spock",
  "NoSpace": "USS Enterprise"
};
myObj["Space Name"];
myObj['More Space'];
myObj["NoSpace"];

myObj[“Space Name”] would be the string Kirk, myObj[‘More Space’] would be the string Spock, and myObj[“NoSpace”] would be the string USS Enterprise.

Note that property names with spaces in them must be in quotes (single or double).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A fragment of code that produces a value is called an ______.

A

A fragment of code that produces a value is called an expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If an expression corresponds to a sentence fragment, a JavaScript ______ corresponds to a whole sentence.

A

If an expression corresponds to a sentence fragment, a JavaScript statement corresponds to a whole sentence.

The simplest kind of statement is an expression with a semicolon behind it.

17
Q

A single let statement may define multiple bindings. The definitions must me separated by ______.

A

A single let statement may define multiple bindings. The definitions must me separated by commas.

let one = 1, two = 2
Console.log(one + two)
// → 3
18
Q

A binding may contain ______ or ______, but no other punctuation or special characters.

A

A binding may contain dollar signs ( $ ) or underscores ( _ ) but no other punctuation or special characters.

19
Q

Applying the ____ operator will convert a value to Boolean type before negating it, and all strings except “” convert to true.

A

Applying the ! operator will convert a value to Boolean type before negating it, and all strings except “” convert to true.

(pg 31)

20
Q

Using the ______ ______ is an easy way to test whether a number is divisible by another number. If it is, the remainder of their division is zero.

A

Using the remainder (%) operator is an easy way to test whether a number is divisible by another number. If it is, the remainder of their division is zero.

console.log(12 % 5);
// expected output: 2
console.log(-12 % 5);
// expected output: -2

Note: While in most languages, ‘%’ is a remainder operator, in some (e.g. Python, Perl) it is a modulo operator. For positive values, the two are equivalent, but when the dividend and divisor are of different signs, they give different results. To obtain a modulo in JavaScript, in place of a % n, use ((a % n ) + n ) % n.

21
Q

Use ______ to immediately jump out of an enclosed loop.

Similarly, The ______ statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

A

Use break to immediately jump out of an enclosed loop.

Similarly, The continue statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.

22
Q

Shortcut for

Counter = counter + 1;

A

counter += 1;

Similar shortcuts work for many other operators, such as result *= 2 to double result or counter -= 1 to count downward.

This allows us to shorten our counting example a little more.

for (let number = 0; number <= 12; number += 2) {
  console.log(number);
}

For counter += 1 and counter -= 1, there are even shorter equivalents: counter++ and counter–.

23
Q

In a switch statement, you may put any number of case labels inside. The program will start executing at the label that corresponds to the value that switch was given, or at ______ if no matching value is found. It will continue executing, even across other labels, until it reaches a ______ statement.

A

In a switch statement, you may put any number of case labels inside. The program will start executing at the label that corresponds to the value that switch was given, or at default if no matching value is found. It will continue executing, even across other labels, until it reaches a break statement.

switch (prompt("What is the weather like?")) {
  case "rainy":
    console.log("Remember to bring an umbrella.");
    break;
  case "sunny":
    console.log("Dress lightly.");
  case "cloudy":
    console.log("Go outside.");
    break;
  default:
    console.log("Unknown weather type!");
    break;
}
24
Q

JavaScript has two ways of writing comments. To write a single-line comment, you can use ______ and then the comment text after it.

For multi-line comments, a section of text between ___ and ___ will be ignored in its entirety, regardless of whether it contains line breaks. This is useful for adding blocks of information about a file or a chunk of program.

A

JavaScript has two ways of writing comments. To write a single-line comment, you can use two slash characters (//) and then the comment text after it.

let accountBalance = calculateBalance(account);
// It's a green hollow where a river sings
accountBalance.adjust();
// Madly catching white tatters in the grass.
let report = new Report();
// Where the sun on the proud mountain rings:
addToReport(accountBalance, report);
// It's a little valley, foaming like light in a glass.

A // comment goes only to the end of the line. A section of text between /* and */ will be ignored in its entirety, regardless of whether it contains line breaks. This is useful for adding blocks of information about a file or a chunk of program.

25
A return keyword without an expression after it will cause the function to return ______.
A return keyword without an expression after it will cause the function to return undefined. Functions that don’t have a return statement at all, such as makeNoise, similarly return undefined.
26
The parentheses after a for keyword must contain two semicolons. The part before the first semicolon ______ the loop, usually by defining a binding. The second part is the expression that checks whether the loop must ______. The final part ______ the state of the loop after every iteration.
The parentheses after a for keyword must contain two semicolons. The part before the first semicolon initializes the loop, usually by defining a binding. The second part is the expression that checks whether the loop must continue. The final part updates the state of the loop after every iteration. In most cases, this is shorter and clearer than a while construct. This is the code that computes 210 using for instead of while: ``` let result = 1; for (let counter = 0; counter < 10; counter = counter + 1) { result = result * 2; } console.log(result); // → 1024 ```
27
Bindings declared with let and const are in fact ______ to the block that they are declared in, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it.
Bindings declared with let and const are in fact local to the block that they are declared in, so if you create one of those inside of a loop, the code before and after the loop cannot “see” it. ``` let x = 10; if (true) { let y = 20; var z = 30; console.log(x + y + z); // → 60 } // y is not visible here console.log(x + z); // → 40 ```
28
JSON
JavaScript Object Notation or JSON is a data interchange format (Related to JavaScript Data Structures) used to store data.