做面试题的时候遇到这样一道题,发现自己对这几个概念还比较模糊,因此做一个总结
面试题目
const name = 'Lydia';
console.log(name());
A: SyntaxError
B: ReferenceError
C: TypeError
D: undefined
TypeErrors get thrown when a value is not of the expected type. JavaScript expected name to be a function since we’re trying to invoke it. It was a string however, so a TypeError gets thrown: name is not a function!
Exp:
let name = "";
console.log(name.reduce()); // TypeError: name.reduce is not a function
SyntaxErrors get thrown when you’ve written something that isn’t valid JavaScript, for example when you’ve written the word return as retrun.
Exp:
le name ="";// SyntaxError: Unexpected identifier
ReferenceErrors get thrown when JavaScript isn’t able to find a reference to a value that you’re trying to access.
Exp
console.log(name); // ReferenceError: name is not defined
Undefined get thrown when a reference is declared but not have a value
Exp
let name;
console.log(name); // undefined