The Basics Flashcards

1
Q

What do data types represent?

A

Different types of data.

They help programmers and their programs determine what they can and cannot do with a given piece of data.

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

What are the 5 primitive data types?

A

String
Number
Undefined
Null
Boolean
*Symbol (ES6)
*BigInt (ES6)

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

If it is not a primitive type?

A

It is an object type

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

If it is a built-in feature of JavaScript, should we use capitalizds or uncapitalized names?

A

Capitalized

*Exception: typeof operator (it always returns a lowercase string value)

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

What is a ‘literal’?

A

A literal is any notation that lets you represent a fixed value in source code

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

String

A

A string is a list of characters in a specific sequence.

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

How do you write string literals?

A

With either single quotes (‘ ‘) or double quotes (“ “)

  • note that the quotes are syntactic components, not part of the value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Escape character or backlash ‘ \ ‘ ?

A

The backslash, or escape character (), tells the computer that the next character isn’t syntactic, but is part of the string

Escaping a quote character prevents JavaScript from seeing it as the end of the string

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

Template literals

A

They use backticks (`) and enable an operation called string interpolation

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

Number

A

The Number data type represents all kinds of numbers in JavaScript

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

Booleans

A

Boolean values represent an “on” or “off” state

There are two boolean literal values: true and false:

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

Undefined

A

When a variable is not defined, its value is given by undefined.

We can describe undefined as representing the absence of a value.

We can also explicitly use the literal undefined.

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

What does ‘console.log()’ return?

A

console.log writes a value to the console.

However, it doesn’t have a reason to return anything specific.

Instead, it returns undefined.

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

Null

A

it represents the intentional absence of a value.

It represents emptiness or nothing

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

Null vs Undefined

A

You must use null explicitly if you want to use it; undefined can arise implicitly.

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

typeof operator

A

typeof returns a string that contains the type of its operand’s value.

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

typeof null

A

‘object’

*Mistake from the first version of JavaScript

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

typeof array

A

‘object’

in JavaScript, arrays are objects (with some special characteristics)

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

Arithmetic operators in JS

A
  1. Addition: ‘ + ‘
  2. Subtraction: ‘ - ‘
  3. Multiplication: ‘ * ‘
  4. Division: ‘ / ‘ (integers or decimals)
  5. Remainder operator: ‘ % ‘ (works with decimal numbers as well)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Remainder operator

A

computes the remainder of dividing two numbers

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

Remainder operator vs Modulo operator

A

Remainder operations return a positive integer when the first operand is positive, and a negative integer when the first operand is negative.

Modulo operations return a positive integer when the second operand is positive, and a negative integer when the second operand is negative.

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

Does JS have an built-in modulo operator or method?

A

NO!

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

NaN

A

This special value signals an illegal operation on numbers

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

What two main situations may cause a NaN result?

A
  1. Undefined mathematical operations, such as dividing 0 by 0 or trying to take the square root of a negative number, return NaN.
  2. Trying to convert a non-number value, such as ‘hello’, to a number can also return NaN.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

typeof NaN

A

‘number’

NaN is a numeric result that indicates an error occurred

26
Q

Why can’t I determine if a value is NaN with regular comparison operator?

A

NaN is the only value in JavaScript that is not equal to itself

27
Q

How can I check if a value is NaN?

A

Number.isNaN or Object.is

28
Q

Infinity

A

Infinity is a number that is so large, it can’t be written down.

One common operation that results in Infinity is to divide a positive number by 0

29
Q

-Infinity?

A

-Infinity represents an infinite value that is less than 0. It most commonly arises when dividing a negative number by 0:

30
Q

typeof Infinity and typeof -Infinity?

A

‘number’ (just like NaN)

31
Q

Can you use comparison operators ‘ === ‘ with Infinity and -Infinity?

A

YES!

However, be wary of using expressions in such comparisons:

32
Q

In JS, how do you compare if two values are identical?

A

With the ‘ === ‘ operator

33
Q

What is string concatenation?

A

The result of using the ‘ + ‘ operator to join two strings

34
Q

What is implicit type coercion?

A

When using +, if either operand is a string and the other is not, JavaScript coerces the non-string operand to a string; thus, the result is always another string.

35
Q

What happens, though, when you perform some other arithmetic operation on a string and number?

A
36
Q

What is explicit type coercion?

A

The difference between explicit and implicit coercion is that explicit coercion lets you decide what you want to do, whereas implicit coercion lets the engine choose.

37
Q

How can I coerce a string to a number?

A

Number function:

38
Q

What happened here?

A

In situations where most other languages raise an error, JavaScript fails silently.

39
Q

Can you use parseInt function to coerce a String into a Number?

A

YES!

A surprising feature of parseInt is how it handles a string whose value begins with a numeric digit or a + or - sign followed by a digit.

In both cases, it returns a numeric value, even though the remaining characters may not be numeric.

It stops converting and ignores everything else once it encounters an invalid character:

40
Q

What does parseInt return if the number in the string is too big or too small to represent as a JS number?

A

It returns Infinity

41
Q

parseFloat

A

It coerces a string to a floating-point (decimal) number.

42
Q

Can you coerce a number into a string?

A

YES!

The String function provides this capability.

43
Q

Arrays (complex data type)

A

JavaScript organizes information into ordered lists using arrays.

They may contain strings, numbers, booleans, or any other data type.

In JavaScript, array literals use square brackets [ ] surrounding a comma-delimited list of values, otherwise known as elements.

44
Q

How do you access elements in arrays?

A

You can access an array element by placing its index inside brackets ( [ ] ).

Element indexes are non-negative integers starting with 0.

45
Q

Trailing comma optional?

A

YES!

But recommended

46
Q

Arrays three most important facts

A
  1. The order of the elements is significant.
  2. Use index numbers to retrieve array elements.
  3. Index numbers are non-negative integers starting from 0.
47
Q

Objects in JavaScript

A

Objects are a dictionary-like data structure that matches keys with specific values.

JavaScript object is a collection of key-value pairs.

48
Q

How do you create an Object?

A

You can create objects using object literals, which have zero or more key-value pairs separated by commas all embedded within curly braces ({}).

A key-value pair associates a key and a given value.

Each pair consists of a key, in the form of a string, and a value of any type.

Key-value pairs in object literals use the key followed by a colon (:) and then the value.

49
Q

How do you retrieve a value in Objects?

A

We retrieve it by its key:

Note how similar the [‘cat’] notation is to indexing with arrays. Instead of index values, though, we use key names in string form.

50
Q

What are expressions in JavaScript?

A

An expression is anything that JavaScript can evaluate to a value, even if that value is undefined or null.

They evaluate to a value that can be captured and used in subsequent code.

51
Q

Do expressions have to use operators?

A

NO!

Expressions don’t have to involve operators: any value is an expression that evaluates to itself:

52
Q

Printing (logging) vs returning values

A

When we invoke the console.log method, we’re telling JavaScript to write something to the console.

Expressions do something, but they also return or evaluate to a value.

53
Q

What happened here?

A

Here, console.log displayed “Howdy” on the console, but then it showed the word undefined in a different color or dimmer brightness.

That extra output is the return value of the expression, and undefined tells you that console.log returned nothing.

It’s important to understand that distinction.

54
Q

What happened here?

A

The value returned by console.log(“Howdy”) is undefined, so that’s the value to which a gets assigned.

Therefore, a on the second line evaluates to undefined, and node shows it as the return value.

55
Q

What are statements in JavaScript?

A

Statements often include expressions as part of their syntax, but the statement itself is not an expression – its value cannot be captured and reused later in your code.

An example of a JavaScript statement is the variable declaration:

On the other hand, the value 3 to the right of the = is an expression

56
Q

Is ‘foo’ here an expression?

A

YES!

57
Q

Statements vs Expressions

A

The key difference between a statement and an expression is that you can’t capture a value from a statement.

For instance, we can capture and print the value of the expression 3 * 5 in the following call to console.log:

58
Q

Can we replace 3 * 5 with a let declaration or while loop?

A

NO! Since they are both statements.

59
Q

What is a statement?

A

A statement is a line of code commanding a task. Every program consists of a sequence of statements.

By this definition, every chunk of code that can be treated as a single unit is a statement.

60
Q

What do statements include?

A
  1. variable, function, and class declarations
  2. loops and if statements
  3. return and break statements
  4. assignments: a = 3;
  5. standalone expressions: console.log(“Hello”);