JS Fundamentals Flashcards
How do JS engines work?
- Parse the script
- Compile script to bytecode
- Executes the bytecode
- used to be a mere interpreter
- now uses a lot of optimizations, e.g. JIT compilation
How do we add JS code to HTML?
- external file:
- inline: console.log(“hello”)
What are statements?
- syntax constructs and commands that perform actions
What roles do semicolons play in JS code?
- separate statements
- maybe omitted in most cases when a line break exists
- > implicit semicolon
What does “use strict” do in JS code?
- directive to enable modern JS (ES5+)
- advanced language structures enable “use strict” automatically, e.g. modules, classes
What is a variable?
- labeled storage for data
What is the difference between let and const?
- let enables to reassign a variable
- const does not allow reassigning a variable
Different data types in JS?
- primitive data types: number, BigInt, string, Boolean, null, undefined, Symbols
- non-primitive data types: Object (array, function)
Outputs of: Number(undefined) Number(null) Number(true) Number(false) Number("")
Number(undefined) // NaN Number(null) // 0 Number(true) // 1 Number(false) // 0 Number("") // 0
Outputs of:
2 + 2 + “1”
“1” + 2 + 2
2 + 2 + “1” // “41”
“1” + 2 + 2 // “122”
What does the unary + do?
- nothing if applied to numbers
- if applied to non-numbers, convert to number
- same behavior as Number(…)
What does the operator = do?
- assigns a value to a variable
- low precedence
- actually returns a value
Difference between postfix and prefix increment/decrement?
counter++
++counter
- postfix: increments and returns old value
- prefix: increments and return new value
How are strings compared in JS?
- letter by letter in lexicographical order
Difference between == and ===?
- ==: loose equality -> compares values with type conversion to numbers
- ===: strict equality -> compares values without type conversion
What does the nullish coalescing operator ?? do
- return first argument if it is not null/undefined, otherwise second one
result = a ?? b result = (a !== null && a !== undefined) ? a : b
Differences between Function Expression and Function Declaration?
Function Expression:
- function created inside an expression or inside another syntax construct
- not hoisted (are created when execution flow reaches them)
Function Declaration:
- function declared as a separate statement in the main code flow
- hoisted (processed before the code block is executed)
What are transpilers?
- software that can parse modern code and rewrite it using older syntax
- enables dev to use modern language syntax features without worrying about backward compatibility
- Babel is one of the most modern
What are polyfills?
- new language features can also contain built-in functions, e.g. Array.flatMap
- polyfills functions that fill the gap of missing functions