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