Generalities Flashcards
How to loop over elements of an array without using an index ?
for(let x of array) {
// use x
}
What is the difference between an expression and a statement ?
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 are called the functions defined using the shorthand syntax of ES6 ?
Arrow functions
What is a litteral ?
It is a data value that appears directly in the program.
Examples:
12
1.2
“Hello World”
‘Hi’
true
false
null
What are the authorize characters to begin an identifier in javascript ?
A letter, an underscore, a dollar sign.
What are the primitive types of javascript ?
number
string
boolean
null
undefined
symbol
bigint
What are the object types of javascript ?
object
functions (and classes)
What are the special kinds of objects (non fundamentals one) defined by Javascript ?
Arrays,
Set,
Map,
Typed Arrays,
RegExp
Date
Error
Even more specials: functions and classes
What does untyped mean in “Constants and variables are untyped”
It means that their declarations do not specify the kind/type of values that will be assigned.
How to write BigInt Litterals ?
As a string of digits followed by the letter ‘n’
What are the differencies between BigInt and Numbers ?
- 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
With the type Date, how to get a string representing the current date in long format ?
Date()
With the type Date, how to get an object representing the current date ?
new Date()
With the type Date, how to get a timestamp representing the current date (number of milliseconds since Jan 1st 1970) ?
Date.now()
or
(new Date()).getTime()
With the type Date, how to get a string representing the current date in long ISO standard format ?
(new Date()).toISOString()
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]
}
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.
It is possible to compare strings with equality (===) or inequality (!==) operators in javascript. True or False
True
What are the differences between for..in and for..of in javascript ?
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.
What does \b means in javascript regex ?
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
In js, how to search a regex in a string with methods of the regex object ?
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.
In js, how to search a regex in a string with methods of the string object ?
text.search(pattern) // => 9: position of first match
text.match(pattern) // => [“1”, “2”, “3”]: array of all matches
What are the falsy values of Javascript ?
undefined
null
0
-0
NaN
“” // the empty string
false
What are the truthy values in javascript ?
All the values that are not one of the 7 falsy values.
WWhat are the differences between null and undefined ?
- 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
What is a symbol ?
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)
What is the global object in javascript ?
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 is the global Object reffered to in Node ?
global
How is the global Object reffered to in a web browser ?
window
How is the global Object reffered to in a web worker ?
self
Since ES2020, what is the standard way to refer to the global object in anycontext (web browser, web worker, node, etc.) ?
globalThis
Type conversions: which values converts to the Number 0 ?
null
false
“”
“0”
[]
[0]
Type conversions: which values converts to the Number NaN ?
[‘a’] // anything that is not a number in the 1st element of the array
function(){}
“one” // non empty string. non numeric
undefined
What is the difference between === and == ?
=== 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.
Does convertibility of a type to another means equality with == ? Why ?
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.
What are the only types that don’t have properties and therefore cannot use either . or []?
null and undefined
How to perform explicit type conversion to primitive types string, number and boolean ?
- 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 to convert a number to a string representing the same number in another base ?
n = 17
base = 2
n.toString(base)
How to format a number so that it appears with exactly n digits after the decimal point
n.toFixed(n)
What does the method Number.toFixed(n) does ?
It formats a number to a string with exactly n digits after the decimal point
What does the method Number.toExponential(n) does ?
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 to formats a number to a string using exponential notation, with 1 digit before the decimal and n digits after the decimal point ?
x.toExponential(n)
What does the method Number.toPrecision(n) does ?
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.
What is the difference between converting a string to number with these 2 methods:
Number(“121323”)
parseInt(“121323”) or parseFloat(“121323”)
parseInt and parseFloat allows more flexibility. In particular with non digit trailing characters at the end of the string.
What is the 2nd argument of the parseInt/parseFloat function ?
It is the radix (or the base). It defines the base in which the string parameter is.