JS Variables Flashcards

1
Q
  1. Which of the following are correct variable declarations?

a. let name = “Max”;
b. let my name = “Jane”;
c. const age = 37;
d. const 1name = ‘Henry’;

A

a
c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Why would you declare variables with const const instead of let?
A

You would use “const” to declare variables whose value is not going to change. On the other hand, you would use “let” to declare variables whose value you expect to change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What would the result of the code below be and why?

const name = “Paul”;
name = “Jeremy”;

A

Error - as it is not possible to assign a new value to a const variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What would the result of the code below be and why?

let country = “Spain”;
const country = “Spain”;

A

SyntaxError - it is not possible to declare two variables with the same name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What would the result of the code below be and why?

const name;
name = “Paul”;

A

Error - a const variable must be initialized during the declaration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Which variable names are valid from this list?

a. _favMovie
b. $sport
c. tv show
d. actorName
e. school name
f. 5days

A

a
b
d

Reminder : Variable Naming Rules

  • Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
  • Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
  • Variable names must not contain spaces.
  • Variable names are case sensitive (Name, name and NAME are all different variables)
  • Reserved keywords cannot be used as variable names.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Declare a variable for your favorite food and name it using camelCase
A

let favoriteFood = “Pizza”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. The following code has some errors, what are they?

const myTown;
myTown = “London”;
myTown = “Madrid”;

A

When variables are declared using const they must be initialized with the value during the declaration, on the same line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. The following code has some errors, what are they?

const myCountry = “Brazil”;
myCountry = “France”;

A

After a “const” variable is declared and initialized, its value cannot be changed by reassignment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is the output of the following console.log?

let hello;

console.log(hello);

A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What would the result of the code below be and why?
    const super = 50;
A

Error because super is a reserved keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Which of the following are true?

a. Variable names are case sensitive
b. Variable names cannot start with numbers
c. Using camelCase notation is mandatory
d. Variables can start with $

A

a
b
d

Reminder : Variable Naming Rules

  • Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
  • Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
  • Variable names must not contain spaces.
  • Variable names are case sensitive (Name, name and NAME are all different variables)
  • Reserved keywords cannot be used as variable names.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What is the output of the following console.log?

let name = “John”;
console.log(“My name is “ + name);

A

My name is John

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Bonus Question: What is the output of the following console.log and why?

let b = 20;
b *= 2;

console.log(b);

A
  1. The operator *=, also known as the multiplication assignment, is used to perform a multiplication and assignment of the result at the same time. If written in the usually way using * and = separately, the expression b *= 2 is written as b = b * 2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Bonus Question: Would this code cause an error? Why/why not?

let y;
y = 9;
y = “Dog”;

A

No, because when a variable is declared using let its value can change.

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

How to create a variable?

A

The actions of creating a variable and giving it some value are known as variable declaration and initialization.

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

What is a variable declaration?

A

A variable declaration is when you create a variable and give it a name.

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

What is a variable initialization?

A

Variable initialization is when you assign some value to a newly declared variable.

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

What operator assignment do you used for a variable ?

A

Assignment operator “=”

The assignment operator = assigns a value specified on the right-hand side to a variable on the left-hand side.

We use the assignment operator = to assign (give) a value to an existing variable. Please do not confuse it with the mathematical equal symbol =, which is used to represent equality.

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

What is happening if you don’t assign a value to a declared variable ?

A

Suppose you declare a variable, but you don’t assign it a value. In that case, its default value will be initialized to undefined

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

When do you use let or const and why ?

A

let is used to declare variables whose value may change in the future.

const is used to declare a variable that will remain constant and whose value can’t be reassigned.

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

What are the variable naming rules ?

A

Reminder : Variable Naming Rules

  • Variable names must begin with a letter, a dollar sign $ , or an underscore _ .
  • Variable names can contain letters (uppercase and lowercase), numbers and the symbols _ and $.
  • Variable names must not contain spaces.
  • Variable names are case sensitive (Name, name and NAME are all different variables)
  • Reserved keywords cannot be used as variable names.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Example for reserved keywords

A

We said we could use any English or non-English word as a variable name. However, JavaScript has certain reserved keywords that cannot be used as variable names. For example, the words let, class, return and function are not allowed as variable names.

break
case
catch
class
const
continue
debugger
default
delete
do
else
export
extends
false
finally
for
function
if
import
in
instanceof
new
null
return
super
switch
this
throw
true
try
typeof
var
void
while
with

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

What are the two categories for Data Types in JavaScript

A

Primitive data types (string, number, boolean, undefined, null, bigInt_ and symbol_)

Non-Primitive data types (object, array)

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

Examples for Primitive Data Types

A

string &raquo_space; let name = “Hello”;
number &raquo_space; let price = 1034;
boolean &raquo_space; let isActive = true;
undefined&raquo_space; let myUndefined = undefined;
null&raquo_space; let myNull = null;

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

Examples for Non-Primitive Data Types

A

object&raquo_space; let student = { name: “Ana”, age: 40 };
array&raquo_space; let numbers = [7, 23, 31, 45, 60];

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

What is the typeof operator ?

A

The typeof operator
At the moment when we create a variable, we define its type. Later on, if we decide we want to assign some other value, we can also change the data type of the value.

In JavaScript, you can reassign values and change the variable’s data type.

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

What is a string?

A

A string is a sequence of characters (text).

A string can contain any character: letter, number, punctuation, or even new lines and tabs.

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

What is a string interpolation?

A

String interpolation is the process of embedding variable values into a string using placeholders.

In JavaScript, string interpolation is available for strings created with backticks `` .

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

What is a multiline string?

A

A multiline string is a long string that spans multiple lines.

In addition to the string interpolation, backticks `` allow you to create multiline strings.

Multiline strings are simply strings that span multiple lines

Strings created with ‘’ and “” quotes don’t permit this and must be written in a single line.

31
Q

What is a concatenation?

A

Concatenation is the process of appending (adding) one string to the end of another string.

32
Q

What is a substring?

A

A substring is a sequence of characters within a string.

Strings have a method .indexOf() used to test the existence of the particular character or a substring in a target string:
> If the substring is found, it returns the index of the substring.
> If the substring is not found, it returns -1.

If a word has the same character/substring two or more times, the indexOf() method will return the index of the first occurrence.

33
Q

How to find a substring ? (method?)

A

We use .indexOf() by providing the character or a substring whose existence we want to check in a string

34
Q

How to extract a substring ? (method?)

A

Extracting a substring - slice()

The string method slice() extracts a part of a string and returns it as a new string without modifying the original string.

Syntax: .slice(start, end)

We use the .slice() method by providing the two indexes that mark the start and the end index of the new string we want to extract.

const statement = “I can’t wait to start the course!!!”;

// Start extracting from the index 0
const substring1 = statement.slice(0, 12);
console.log(substring1); // ==> I can’t wait

// If the second parameter is not supplied, it returns the substring
// starting from the position specified until the end of the string.
const substring2 = statement.slice(16);
console.log(substring2); // ==> start the course!!!

// If a negative number is passed the extraction begins from the end of the string
const substring3 = statement.slice(-9);
console.log(substring3); // ==> course!!!

35
Q

What does the method .toUpperCase()

A

The string method .toUpperCase() converts all the letters in a string to uppercase.

As you can see, to use .toUpperCase() and any other JavaScript method we must use () at the end.

string.toUpperCase();

const name = “sandra”;

// Get the first character and uppercase it
const firstCharUppercased = name[0].toUpperCase();

// Concatenate uppercased letter and the rest of the string
const nameFormatted = firstCharUppercased + name.slice(1);

console.log(firstCharUppercased); // ==> S
console.log(nameFormatted); // ==> Sandra

36
Q

What are representing numbers in JS ?

A

In JavaScript, numbers can be represented as integers or floating-pointnumbers:

  • Integer: a whole number, such as1,12,256,10000,5,83, etc.
  • Floating-point number: a number with a decimal point, such as0.2,5.5,123.55, etc
37
Q

Example of a floating-point number?

A

Floating-point number: a number with a decimal point, such as0.2,5.5,123.55, etc

38
Q

Example of an integer number?

A
  • Integer: a whole number, such as1,12,256,10000,5,83, etc.
39
Q

What is Exponentiation?

A

Exponentiation is a mathematical operation, in which we take a quantity b (the base) to the power of another quantity e (the exponent).

Or in simpler words: it is the process of multiplying b (the base) by itself as many times as indicated by e (the exponent). The operation is commonly denoted as be.

In JavaScript, you can easily apply exponentiation by using the exponentiation operator **:

console.log(2 ** 3); // => (2 * 2 * 2) result: 8

console.log(3 ** 3); // ==> (3 * 3 * 3) result: 27

40
Q

In JS, how to apply exponentiation?

A

In JavaScript, you can easily apply exponentiation by using the exponentiation operator **:

console.log(2 ** 3); // => (2 * 2 * 2) result: 8

console.log(3 ** 3); // ==> (3 * 3 * 3) result: 27

41
Q

What is Modulo ?

A

Modulo (%) is the remainder operator. Think of this as saying If I divide the first number by the second, what is the remainder?

The modulo (or “modulus”) operator returns the remainder after dividing one number by another. The remainder result is rounded to a whole number (without decimals).

The modulo operator is very handy for finding multiples of a particular number.

42
Q

How to apply modulo ?

A

with %

43
Q

How to check if number is even or odd ?

A

Check if number is even or odd
Most commonly, the modulo operator is used to check if a number is even or odd.

To simplify checking if a number is even or odd, use the following formulas:

Even % 2 = 0 - If the remainder after division by 2 is equal to 0, a number is even.
Odd % 2 = 1 - If the remainder after division by 2 is equal to 1, a number is odd.

44
Q

Example of Advanced Assignment Operators

A

Assignment operators (+=, -=, *=, etc.) calculate and assign the value of an operation to a variable on the same line.

They allow us to perform calculations and assignments on the same line.

Example:
let yourAge = 25;

yourAge += 1; // same as: yourAge = yourAge + 1

console.log(yourAge); // ==> 26

45
Q

What is an expression?

A

An expression is a combination of any values (number, string, array, object) and operators that produces another value.

An expression is any unit of code that produces a value when evaluated. An expression can contain any combination of literal values, variables, operators, function invocations and even other expressions that can evaluate to a single value.

example : const result = 2 + 4;

46
Q

What the name for a combination of any values and operators that produces another values?

A

An expression.
Can contain any combination of literal values, variables, operators, function invocations and even other expressions that can evaluate to a single value.

example : const result = 2 + 4;

47
Q

What is a Boolean data type?

A

Boolean data type can have one of two possible values: true or false.

Booleans are used to represent two states, such as if a light is ON or OFF, or to indicate if something is true or false.

Boolean is a primitive data type in JavaScript. Boolean values are used in computer programming to create conditions and alter the control flow of the program. These conditions are used to determine whether or not some code should be executed.

Boolean can have one of two possible values: true or false. These two values can also be represented in binary as 1 (true) and 0 (false).

48
Q

What do you use a comparison operators?

A

Comparison operators are used to check if two values or variables are equal or different.

49
Q

What is the difference between == and === ?

A

Strict Equality ===
As you may have noticed the operators == and === work in a seemingly similar way. However, there is an important difference:

== only checks if the values are the same.
=== checks if the values are the same and it also checks if values are of the same data type.

When comparing values, using == might introduce unexpected bugs. Therefore, it is recommended (and a best practice) to always use === . The same goes for !==.

50
Q

What is a logical operator ?

A

Logical operators are used for combining two or more boolean expressions (expressions that return true or false) into one large expression that returns true or false.

51
Q

What are the logical operators ?

A

Logical operators allow us to combine boolean expressions. They may also be used with simple boolean values true and false. There are three different logical operators: AND (&&), OR (||), NOT (!). Logical operators take two expressions (left and right), commonly called operands.

AND (&&) expr1 && expr2 Returns true if both operands are true; otherwise, returns false.
OR ( ||) expr1 || expr2 Returns true if at least one of the operands is true; if both are false, returns false.
NOT (!) !expr Returns the opposite boolean state. If the operand is true, returns false. If the operand is false, returns true.

52
Q

Logical AND (&&)

A

Logical operator AND && checks the operands from left to right.

It returns true if both operands are true; otherwise, returns false.

53
Q

Logical OR (||)

A

Logical operator OR || checks the operands from left to right.

It returns true if at least one of the operands is true; if both operands are false, returns false.

54
Q

Logical NOT (!)

A

Logical operator NOT ! (also called negation), reverses a boolean value into its opposite.

If the value is true it converts it to false.
If the value is false it converts it to true

55
Q

What is Boolean context ?

A

Boolean context means that a value is used with the logical operators or in a conditional statement (more on this later). When a non-boolean value is evaluated in a boolean context, JavaScript automatically converts (coerces) into true or false. We call these values truthy and falsy.

When you say that a value is truthy in JavaScript, it doesn’t mean that the value is true. Instead, it means that the value will be converted to true when evaluated in a boolean context.

Correspondingly, when you say a value is falsy in JavaScript, it means the value will be converted to false when evaluated in a Boolean context.

56
Q

What are truthy values?

A

Truthy values, are values that JavaScript automatically converts to true, when in a boolean context.

All values in JavaScript are truthy except false, 0, -0, 0n, “”, null, undefined and NaN.

57
Q

What are falsy values?

A

Falsy values, are values that JavaScript automatically converts to false, when in a boolean context.

false The boolean value false
0 The number zero
-0 The number negative zero
“”, ‘’, `` Empty string of any kind
NaN NaN or Not a number is special value indicating an incorrect numerical operation. For example, dividing a number by a string (2 / “bob”) generates NaN.
null Represent the absence of any value.
undefined Special value that JavaScript automatically assigns to uninitialized variables.

58
Q

What is the purpose of the Data type undefined?

A

The primitive data type undefined is a JavaScript value with a special purpose. If a variable is declared, but not initialized JavaScript will automatically set its value to undefined. It is treated as falsy when in a boolean context.

59
Q

What does represent Data type null?

A

The primitive data type null represents the intentional absence of a value. We commonly assign it to a variable to indicate that the variable has been left empty on purpose.

The value null is treated as falsy when used in a boolean context.

60
Q
  1. Select the falsy values from the list:
    a. “”
    b. -10
    c. null
    d. 20.60
    e. 0
    f. undefined
    g. “hello”
A

a
c
e
f

61
Q
  1. What is the output of the following console.log and why?
    let y = false;
    console.log(y === false);
A

True - as let y = false and false is equal to false

62
Q
  1. The data types null and undefined are one and the same:

a. True
b. False

A

False
null Represent the absence of any value.
undefined Special value that JavaScript automatically assigns to uninitialized variables.

63
Q
  1. What is the output of the following code?
    let film = “Batman”;
    let result = film || “Show”;
    console.log(result);
A

Batman

To summarize, the code sets the value of the film variable to “Batman”. Then, it uses the OR operator to assign the value of film to the result variable. Finally, it displays the value of the result variable, which is “Batman”.

64
Q
  1. What is the output of the following console.log and why?

let y;
console.log(y === “happy”);

A

False, since y is undefined and when compared with the === expression, it returns false as y is not equal to the string “happy”.

65
Q
  1. What is the output of the following console.log and why?
    let food;
    console.log(food === undefined);
A

True, since the variable food was declared but not initialized, therefore having the value of undefined.

66
Q
  1. What is the output of the following console.log and why?

let x = “10”;
let y = 10;
console.log(x !== y);

A

True - since the variables x and y have different values and data types.

Reminder : exp1 !== exp2 Returns true if the expressions do not have the same value or the same type of value.

67
Q
  1. What is the output of the following console.log and why?
    let x = “20”;
    let y = 20;
    console.log(x == y);
A

True - since when using the == operator, data types do not matter so the values of x and y are equal.

68
Q
  1. Which is the boolean value of the statement 6 < 2?

a. false
b. true
c. Yes
d. null
e. undefined

A

a. False

69
Q
  1. Which of the following are logical operators?

a. &&
b. >
c. ≤
d. ||
e. !=
f. +

A

a
d

70
Q
  1. How is the logical operator NOT written?

a. !!
b. ||
c. !
d. &&
e. &&&

A

c

71
Q
  1. If c is false what is the result of the following boolean expression?

!c

A

True

Reminder : NOT (!) !expr Returns the opposite boolean state. If the operand is true, returns false. If the operand is false, returns true.

72
Q
  1. If d is true and e is true what is the result of the following expression?

d && e

A

True

Reminder : AND (&&) expr1 && expr2 Returns true if both operands are true; otherwise, returns false.

73
Q
  1. If a is false and b is true what is the result of the following expression?
    a || b
A

True
Reminder : OR ( ||) expr1 || expr2 Returns true if at least one of the operands is true; if both are false, returns false.