Generalities Flashcards

1
Q

How to loop over elements of an array without using an index ?

A

for(let x of array) {
// use x
}

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

What is the difference between an expression and a statement ?

A

Basically, expressions represent anything that is a value or that will produce a value.

A statement is a task, an order. In general the statement modify the state of the computer. And expression can produce a value without modifying the state of the computer.

A statement is formed by one or many expressions. A statement can also itself be an expression.

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

How are called the functions defined using the shorthand syntax of ES6 ?

A

Arrow functions

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

What is a litteral ?

A

It is a data value that appears directly in the program.

Examples:
12
1.2
“Hello World”
‘Hi’
true
false
null

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

What are the authorize characters to begin an identifier in javascript ?

A

A letter, an underscore, a dollar sign.

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

What are the primitive types of javascript ?

A

number
string
boolean
null
undefined
symbol
bigint

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

What are the object types of javascript ?

A

object
functions (and classes)

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

What are the special kinds of objects (non fundamentals one) defined by Javascript ?

A

Arrays,
Set,
Map,
Typed Arrays,
RegExp
Date
Error

Even more specials: functions and classes

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

What does untyped mean in “Constants and variables are untyped”

A

It means that their declarations do not specify the kind/type of values that will be assigned.

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

How to write BigInt Litterals ?

A

As a string of digits followed by the letter ‘n’

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

What are the differencies between BigInt and Numbers ?

A
  • BigInt are arbitrary precision, Numbers have the limits imposed by IEEE754
  • Numbers can represent floating point numbers and BigInt can only represent Integers
  • Both types cannot be mixed in arithmetic operations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

With the type Date, how to get a string representing the current date in long format ?

A

Date()

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

With the type Date, how to get an object representing the current date ?

A

new Date()

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

With the type Date, how to get a timestamp representing the current date (number of milliseconds since Jan 1st 1970) ?

A

Date.now()
or
(new Date()).getTime()

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

With the type Date, how to get a string representing the current date in long ISO standard format ?

A

(new Date()).toISOString()

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

With strings, what is the difference between

for(let x of aString) {
// use x
}

and

for(let i=0; i<aString.length; i++) {
// use aString[i]
}

A

the first method iterate on unicode characters, the second iterate on 16bit elements of the string. The difference is that some unicode characters may require more than 1 16bit element in the string. These characters will be returned in 2 iterations with the second method.

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

It is possible to compare strings with equality (===) or inequality (!==) operators in javascript. True or False

A

True

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

What are the differences between for..in and for..of in javascript ?

A

for in loops over enumerable property names of an object.

for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.

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

What does \b means in javascript regex ?

A

The \b metacharacter matches at the beginning or end of a word.

Search for the pattern LO at the beginning of a word like this:
\bLO

Search for the pattern LO at the end of a word like this:
LO\b

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

In js, how to search a regex in a string with methods of the regex object ?

A

pattern.test(text) => return true or false

pattern.exec(text) => return null or an array. The first elt of the array is the matching string.

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

In js, how to search a regex in a string with methods of the string object ?

A

text.search(pattern) // => 9: position of first match

text.match(pattern) // => [“1”, “2”, “3”]: array of all matches

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

What are the falsy values of Javascript ?

A

undefined
null
0
-0
NaN
“” // the empty string
false

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

What are the truthy values in javascript ?

A

All the values that are not one of the 7 falsy values.

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

WWhat are the differences between null and undefined ?

A
  • null is a keyword, undefined is a constant
  • typeof null == “object” (for historical reason) and typeof undefined == “undefined”
  • null has to be explicitly set. Undefined is the default value when a variable, a constant, a property, etc has not been initialized
  • In a conversion to Number, null converts to 0 and undefined converts to NaN
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is a symbol ?

A

It is a type of property key that is not a string (as usual). Symbols allows to be sure that the property name has not been previously chosen (is unique)

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

What is the global object in javascript ?

A

It is a regular object that serves a very important purpose: the properties of this object are the globally defined identifiers that are available to a javascript program:

Constants like undefined, Infinity, NaN
Functions like parseInt(), isNaN()
Constructors like Date(), RegExp(), String()
Objects like Math, JSON, etc.

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

How is the global Object reffered to in Node ?

A

global

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

How is the global Object reffered to in a web browser ?

A

window

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

How is the global Object reffered to in a web worker ?

A

self

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

Since ES2020, what is the standard way to refer to the global object in anycontext (web browser, web worker, node, etc.) ?

A

globalThis

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

Type conversions: which values converts to the Number 0 ?

A

null
false
“”
“0”
[]
[0]

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

Type conversions: which values converts to the Number NaN ?

A

[‘a’] // anything that is not a number in the 1st element of the array

function(){}

“one” // non empty string. non numeric

undefined

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

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

A

=== does not consider its operands to be equal if they don’t have the same type.

== is more flexible and may perform some type conversions of its operands before comparing the results. As a result, values of different types might be considered as equals.

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

Does convertibility of a type to another means equality with == ? Why ?

A

No.

Example:
undefined == false // returns false

but

! undefined // will be evaluated to true, because conversion of undefined to boolean is false.

Because each operator has its own rules to converts its operands from one type to another type (chosen by the operator) before performing its operation. the == operator never attempts to converts its operand to booleans. It rather attempt to convert them into Integer and undefined converts to NaN while false converts to 0.

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

What are the only types that don’t have properties and therefore cannot use either . or []?

A

null and undefined

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

How to perform explicit type conversion to primitive types string, number and boolean ?

A
  • Use the wrapper functions: String(1), Number(“1”), Boolean(2) - without new. Otherwise a wrapper object is created.
  • Using operators that perform implicit type conversions: like the unary operator + (convert it operand to a number), the binary operator + when one of its operand is a string will convert the other operand to a string, the ! unary operator converts its operand to a boolean and negates its, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

How to convert a number to a string representing the same number in another base ?

A

n = 17
base = 2
n.toString(base)

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

How to format a number so that it appears with exactly n digits after the decimal point

A

n.toFixed(n)

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

What does the method Number.toFixed(n) does ?

A

It formats a number to a string with exactly n digits after the decimal point

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

What does the method Number.toExponential(n) does ?

A

It formats a number to a string using exponential notation, with 1 digit before the decimal and n digits after the decimal point. The number of significant digits is then n+1

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

How to formats a number to a string using exponential notation, with 1 digit before the decimal and n digits after the decimal point ?

A

x.toExponential(n)

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

What does the method Number.toPrecision(n) does ?

A

It converts the number to string with n significant digits. It uses exponential notation if n is not enough to display the entire integer portion of the number.

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

What is the difference between converting a string to number with these 2 methods:

Number(“121323”)
parseInt(“121323”) or parseFloat(“121323”)

A

parseInt and parseFloat allows more flexibility. In particular with non digit trailing characters at the end of the string.

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

What is the 2nd argument of the parseInt/parseFloat function ?

A

It is the radix (or the base). It defines the base in which the string parameter is.

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

What are the 3 fundamental algorithms the javascript specification defines for converting Objects to Primitives ?

A
  • prefer-string: This algorithm returns a primitive value, preferring a string, if a conversion to string is possible
  • prefer-number: This algorithm returns a primitive value, preferring a number, if such conversion is possible.
  • no-preference: This algorithm expresses no preference about what type of primitive value is desired, and classes can define their own conversion.
46
Q

What is the object-to-primitive conversion algorithm preferred by all built-in javascript types except Date ?

A

prefer-number

47
Q

What is the object-to-primitive conversion algorithm preferred by the class Date ?

A

prefer-string

48
Q

Describe what happens when an object needs to be converted to a boolean

A

It is always true. Whatever the object is ({}, [], new Boolean(false), etc.)

49
Q

Describe what happens when an object needs to be converted to a string

A

JavaScript first converts this object to a primitive using the prefer-string algorithm, then converts the resulting value to a string if necessary

50
Q

List some examples of situations where an object needs to be converted to a string

A

If you pass an object to a built-in fonction that expect a string argument

If you call String() as a conversion function

When you interpolate objects into template litterals.

51
Q

Describe what happens when an object needs to be converted to a number

A

JavaScript first converts this object to a primitive using the prefer-number algorithm, then converts the resulting value to a number if necessary

52
Q

List some examples of situations where an object needs to be converted to a number

A

Built-in functions and methods that expect a numeric arguments

most javascript operators that expects numeric operands

53
Q

Describe how the binary + operator perform its type conversion

A

If either of its operand is an object, JavaScript converts them to primitive values using the “no-preference” algorithm. Once it has 2 primitive values, it checks their types.

If either argument is a string, it converts the other to a string and concatenates the strings.

Otherwise it converts both arguments to numbers and adds them

54
Q

What are the special case operators that do not use the basic object-to-string or object-to-number conversions ?

A

The binary operator +

The equality (==) and inequality (!=) operators

The relational operators (<, <=, >, >=)

55
Q

Describe how the == and != operators perform their Object to Primitive conversion

A

If one operand is an object and the other is a primitive value, the object is converted to a primitive using the “no-preference” algorithm and then the comparison is made between the 2 primitives

56
Q

Describe how the relational operators (<, <=, >, >=)perform their Object to Primitive conversion

A

If either operand is an object, it is converted to a primitive value using the “prefer-number” algorithm. The obtained value will not be converted to a number.

57
Q

Explain why is it possible to compare date objects in javascript ? d1 < d2

A

Because of the way relational operators perform type conversions of their operands.

As date objects are objects, they will be converted to primitive using the “prefer-number” algorithm. The prefer-number algorithm applied to a date object will return an integer representing the number of milliseconds passed since EPOCH.

58
Q

What is the difference between toString() and valueOf()

A
  • The job of toString() is to return a string representation of the object.
  • The job of “valueOf()” is less well defined: it is supposed to convert the object into a primitive that represent the object, if any such primitive value exists. The default valueOf() method simply returns the object itself rather than returning a primitive.
59
Q

What are the differences between let/const and var ?

A
  • Let/const variables are always blocked scope. Whereas var variables are either function scoped (when declaration is done into a function) or global (when declaration is done outside the function).
  • Global variables declared with let are different from global variable declared with var. Indeed the latest are implemented as properties of the Global Object.
  • It is legal to declare the same variable multiple times with var. Which is not the case for let/const.
  • Used in the code passed to the eval function, let/const create variable that exist only in the local evaluation of that code, howeve var defines new variables in the calling environment or even global variables (when eval is called from another function name)
60
Q

What is the difference between global variables declared with var, and properties of the global Object ?

A

The properties created with global var declarations, are properties of the global Object, but connot be deleted with the delete operator.

61
Q

What is the difference with global variables declared with var and undeclared variables ?

A

Undeclared variables (outside of strict mode) become properties of the global object, but they can be deleted with the delete operator

62
Q

What does the function Object.entries ?

A

It takes 1 argument, an object, and return an array containing arrays of couple “key”, “values”

63
Q

What is desctructuring assignment ?

A

It is an assignment in which, the value on the righthand side of the equal sign is an object or an array or an array of objects or an object of arrays (a structured value) and the lefthand side specifies one ore more variable names using a syntax that mimics array and object literal syntax.

e.g.

[x,y] = [1, 2] // same as x=1; y=2;
[x, …y] = [1,2,3,4] // same as x=1, y=[2,3,4]
{y, x} = {x: 1, y: 2} // same as y=2; x=1;

64
Q

What is the difference between true litterals and object/array litterals ?

A

True litterals are primary expressions.
Object/Arrays Litterals are not, because they may include a number of subexpressions that specify property and element values.

65
Q

What is optional chaining ?

A

Also called, conditional property access, it is a way to access properties of an object or an array, using the operators ?. or ?[].

This prevent throwing a TypeError when the object to be accessed (at the left of ?. or ?.[]) is null or undefined.

66
Q

What are the types of expressions in javascript ?

A
  • Primitive expressions (litterals, some default keywords, identifiers - variables/constants/etc.)
  • Object/Arrays Initializers or Litterals
  • Function Definition Expressions (Function Litterals)
  • Property Access expressions (.identifier, [“propertyname”], optional chaining)
  • Invocation expressions (function calls)
  • Object creation expressions (new Constructor())
67
Q

What is a method in javascript ?

A

A method is a function that is a property of an object.

68
Q

What is conditional invocation ?

A

It is optional chaining applied to function invocation using the operator ?.()

69
Q

What is an lvalue ?

A

lvalue is a historical term that means “an expression that can legally appear on the left side of an assignment expression”

e.g. variables, properties of objects, elements of arrays

70
Q

What are the operators that produce side effects in javascript ?

A
  • assignment operators (=, +=, *=, etc.)
  • incrementation and decrementation operators (++, –)
  • delete
71
Q

Property access and invocation expressions have the highest possible precedence. TRUE or FALSE

A

True

72
Q

What is the associativity of an operator ?

A

It is the order in which operations of the same precedence are performed : either left to right or right to left.

73
Q

What is the javascript order of evaluation of expressions ?

A

Always left-to-right.

It differs from the order in which operations are performed in a complex expression. This is the order of evaluation of subexpressions in the expression.

74
Q

What are the arithmetic operators ?

A
  • The exponentiation **
  • The multiplication *
  • The division /
  • The addition +
  • The subtraction -
  • The bitwise operators (&, |, ^, ~,&raquo_space;, «,&raquo_space;>)
75
Q

How do arithmetic operators, except the addition, perform type conversion ?

A

They convert their operands to numbers if necessary, and then compute the power/multiplication/etc.

76
Q

List the relational operators

A
  • Equality or Inequality (==, ===, !=, !===)
  • Comparison (<, >, <=, >=)
  • in
  • instanceof
77
Q

What happen when either one operand of a comparison operator is NaN ?

A

The result of the operation is false

78
Q

Describe in details how the strict equality (===) works ?

A

SUNBNSO

  • If operands are not of the Same type, then they are not equal.
  • If both operands are Undefined, or both operands are Null, then they are equal.
  • If both values are the Boolean true, or both values are the boolean false, then they are equals.
  • If one of the operand is NaN, then they are not equal.
  • If both values are Numbers and have the same value (or one is 0 and the other -0), then they are equal.
  • if both operands are String, then string comparison is performed and result is returned.
  • if both operands are Object, they are equal only if they refer to the same object. Otherwise they are not equal.
79
Q

Describe in details how the Equality with type conversion (==) works ?

A

SUNS N BONS

  • If both operands have the Same type, compare them with the strict equality and return the result.
  • If one operand is Undefined and the other is Null, then they are equal
  • If one operand is a String and the other is a Number, converts the string to a number and start from the beginning again using the converted value.
  • (Boolean)If either value is true, converts it to 1. If either value is false convert it to 0. and start from the beginning again.
  • If one operand is an Object and the other is a Number or a String, convert the object to a primitive using the “no-preferrence algorithm” and start again from the beginning.
  • In any other case, return false.
80
Q

Describe in details how other comparison operators work (<, >) work ?

A
  • If either operator is an object, convert this object to a primitive using the “prefer number” algorithm.
  • If after object to primitive conversion both operands are string, perform string comparison and return the result
  • If after object to primitive conversion at least one operand is not a string, both operands are converted to number and then compared numerically.
81
Q

The logical operators &&, || and ! convert their operands to boolean before applying the requested operation. True or False. Explain.

A

False.

&& and || do not convert their operands. They rather evaluate them as “truthy” or “falsy” values and return also a “truthy” or “falsy” value.

However, ! converts its operands to a boolean before inverting the converted value.

82
Q

what is the difference between a direct call to eval, an indirect call to eval and a strict eval ?

A

Out of strict mode:

A direct call to eval is call to the eval function using the function name “eval” itself. In this way, the code is evaluated using the variables calling environment as its environment. New variables declared with var will be created in the calling context. And access to the variables available in the local calling context is possible.

An indirect call to eval is a call to the eval function done using an alias (let alias=eval). In this way, the code is evaluated using the global object as its variables environment and cannot read, write or define local variables or functions.

In strict mode

Strict eval does an eval using a private variable environment. It is possible to query and set local variables, but it is impossible to define new variables or function in the local scope. Also, in this mode it is not allowed to overwrite the eval function with a new value, or to use the work “eval” as an identifier.

83
Q

How is the operator ?? called ?

A

First Defined

84
Q

What does the first defined operator (??) do ?

A

a ?? b
It returns the first of its operand that is defined and not null.

85
Q

What is the difference between ?? and || ?

A

|| will return the second operand only if the first one is falsy (7 values are falsy: 0, NaN, “”, false, -0, null, undefined)

?? will return the seconde operand only if the first one is either null or underfined.

86
Q

what are the possible values typeof can return ?

A

object
undefined
number
string
boolean
symbol
bigint
function

87
Q

Describe briefly how the delete operator work ?

A

Delete expects its operand to be an lvalue.
If it is not an lvalue, the operator takes no action and returns true.
Otherwise delete attempt to delete the specified lvalue and returns true if it succeed.

If the lvalue doesn’t exists, delete returns undefined

if the lvalue is not deletable, delete raise an exception.

88
Q

What does the void operator do ?

A

It evaluates its operand, then discard the value and return undefined.

89
Q

What does the comma operator do ?

A

It is a binary operator that evaluates its left operand, evaluates its right operand and then return the value of the right operand.

90
Q

What is a compound statement ?

A

It is a statement block. A sequence of statements enclosed within curly braces

{
x=1;
y=2;
}

91
Q

What is the empty statement ?

A

It is the ; alone.

It allows to include no statement where one is expected.

;

92
Q

How to get the array of all the keys (property names) of an object ?

A

Object.keys(obj)

93
Q

How to get the array of all the values (property values) of an object ?

A

Object.values(obj)

94
Q

How to get the array of all the keys/values of an object ?

A

Object.entries(obj)

95
Q

What is the syntax of the try/catch/finally statement ?

A

try {

} catch (e) {

} finally {

}

catch and finally blocks are optional

the “(e)” part of the catch line is optional

The finally block is always executed, whatever happen in try and/or catch blocks above.

96
Q

Describe the “with” statement

A

with (object)
statement

with runs a block of code as if the properties of the specified object were variables in scope of that code.

with should be considered as deprecated.

97
Q

Which primitive types can behave like immutable objects ?

A

strings, number, boolean

98
Q

How are properties defined directly on an object (not inherited ones) called ?

A

Own properties

99
Q

In addition to its name and value, which are the 3 attributes of a property ?

A
  • enumerable: specifies whether the property name is returned by for/in loop
  • writable: specifies whether the value of the property can be set
  • configurabe: specifies whether the property can be deleted and wether its attributes can be altered.
100
Q

In addition to its name and value, which are the 3 attributes of a property ?

A
  • enumerable: specifies whether the property name is returned by for/in loop
  • writable: specifies whether the value of the property can be set
  • configurabe: specifies whether the property can be deleted and wether its attributes can be altered.
101
Q

What are the 3 ways to create objects in javascript ?

A
  • Object Litteral
  • Call the create function
  • Using new
102
Q

What is an object litteral ?

A

An object litteral is an expression that creates and initializes a new and DISTINCT object each time it is evaluated

103
Q

How to test the existance of property in an object ?

A
  • the in operator
  • theObject.hasOwnProperty(“p”)
  • theObject.propertyIsEnumerable(“p”)
  • theObject.p !== undefined
104
Q

What is the difference between the in operator and theObject.hasOwnProperty ?

A

in will return true if the property is an own property, an inherited property whether enumerable or not.

Object.hasOwnProperty will return true only if the property is an own property (enumerable or not). Inherited property will return false.

105
Q

How to enumerate properties of an object ?

A
  • For/in loop
  • Object.keys()
  • Object.getOwnPropertyNames
  • Object.getOwnPropertySymbols
  • Reflect.ownKeys()
106
Q

theObject.propertyIsEnumerable

VS

theObject.hasOwnProperty

A

The first is the second + check if the property is enumerable.

107
Q

Which subtle difference exists between the in operator when testing property existence and the for/in loop when enumerating properties ?

A
  • The in operator will return true even if the property in non enumerable (whether own or inherited)
  • The for/in loop only loop over the enumerable properties (whether own or inherited)
108
Q

What the function Reflect.ownKeys does ?

A

This function returns all own property names, both enumerable and non enumerable and both string and symbol

109
Q

How to copy all the properties of an object objB into an object objA

A

Object.assign(objA, objB)

it copies all the enumerable own properties of objB into objA.
It can take multiple parameters objB, objC, etc. All parameters from the 2nd one are source objects whose properties are to be copied.

110
Q

How is the method toJSON() used ?

A

JSON.stringify(obj) looks for a toJSON method in obj.

If this method exists, it is invoked, and its returned value is serialized instead of obj.