General Primitive Knowledge Flashcards

1
Q

typeof 23.5;

typeof ‘Call me Ishmael.’;

typeof false;

typeof 0;

typeof [ ]

typeof { }

typeof null;

typeof undefined;

typeof Infinity

A

number

string

boolean

number

object

object

object

undefined

number

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

Why does typeof null return “object”

A

Basically a legacy mistake.

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value.

The type tag for objects was 0.nullwas represented as the NULL pointer (0x00 in most platforms).

Consequently, null had 0 as type tag, hence the “object”typeofreturn value.

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

Name all the data types

How many are there?

A

8

Number
Boolean
String

Symbol
BigInt

null
undefined

Object

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

What are the data type categories?

How do the data types fit into them?

A
Primitive and composite
Primitive data types
1 Number
2 String
3 Boolean

4Symbol
5 BigInt

6 Null
7 undefined

Everything else is an object or Composite data type
8 Object
Array
Functions

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

What makes a composite data type?

What makes a primitive?

A

Composite typesare made up of heterogeneous data as one unit.

A composite type can contain not only strings, numbers, Booleans,undefinedvalues, and null values, but even other composite types.

JavaScript supports three composite types: objects, arrays, and functions.

A primitive in JS is defined as NOT an object

When a primitive value is assigned to a variable (eg let foo = ‘bar’), the variable is set to that value directly.
An object variable value is set to the destination in memory where the object data is stored.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explicitly change data

  • return argument as a number (2 main ways)?
  • return argument as a string, 2 ways
  • return an integer if it’s in the argument
  • return a float if it’s in the argument
A

Number(‘424’), +’1’

String(4124),
Object.prototype.toString(number base)
ex (12312).toString(10);

Number.parseInt( )
Number.parseFloat( )
parseInt and parseFloat find the first number possible:Number.parseFloat(‘11.11fda2 1’ //returns 11.11

But they will return NaN if a non number comes first::Number.parseFloat(‘a11.11’) //returns NaN

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

Where are primitive and reference values stored?

A

Primitive values are data that are stored on the stack. Primitive value is stored directly in the location that the variable accesses.

Reference values are objects that are stored in the heap.

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

Can you change a primitive?

A

No, they can be created and destroyed but not changed like objects

(which means there are no mutating methods for strings, numbers, and boolean.

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

Check if something is specifically NaN

A

Number.isNaN()

or

let isNaN = function(x) {

return !(x===x);

}

(NaN is the only thing that is not equivalent with itself)

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

Write a function that checks whether the input is specifically NaN

A

NaN is the only thing that does not equal itself function isItNaN(input) { if (input !== input) { console.log(true); } else { console.log(false); } }

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

What happens:

let b = (a = 1);

console. log(b);
console. log(a);

Why? (Incomplete)

A

evaluates to:

1

1

The a variable is declared n.

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

Why doesn’t NaN === NaN evaluate to true?

A

NaN is designed to propagate through all calculations, infecting them like a virus, so if somewhere in your deep, complex calculations you hit upon a NaN, you don’t bubble out a seemingly sensible answer.

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

null == undefined

A

true

but null === undefined evaluates to false

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

When using > < >= and <=

What happens with null and undefined?

A

they are converted to numbers

(same as +null or Number(null) etc.)

(null to 0, undefined to NaN)

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

alert( undefined > 0 );
alert( undefined < 0 );
alert( undefined == 0 );

A

// false (1)

// false (2)

// false (3)

  • undefined is converted to NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are null and undefined different from other primitives

A

Null and undefined, have no wrapper type like strings, booleans and numbers do. If you try t o assign a value to them it will throw an error.

You can with NaN. Like other primitives, it won’t stick but it won’t throw an error.

17
Q

How do you assign a property to a primitive?

A

You can’t (or at least we don’t know how to yet)
Primitive types behave like objects when you use the property access notations, but you can’t assign new properties to them. Primitives get wrapped with an object temporarily, and then that object is immediately thrown away. Any attempt to assign values to properties will seem to succeed (even Nan), but subsequent attempts to access that new property will fail.

You can, however, assign them to String, Object, and Number. Because they are proper objects. They don’t just receive a wrapper like primitives.

18
Q

Are undefined, null and NaN primitives?

What does typeof return for undefined, null and NaN

A

Undefined, Null and NaN are primitive.

typeof null will return object, but it is a primitive

typeof NaN will return number

typeof undefined will return undefined

19
Q

‘beta’ < ‘gamma’

A

true

20
Q

[1,2] < [6, 7]

[12,2] < [6]

[72,2] < [6]

A

true

true

false

Compares them like strings

21
Q

{a: ‘a’} < {z: ‘z’}

{a: ‘a’} > {z: ‘z’}

A

false

false