为什么JavaScript会继续打印语法错误,而不是在我的终端中打印以前的类型错误



//declare and assign
const firstName = 'bangalore'
console.log(firstName)
//reassign
firstName = 'mysore'
console.log(firstName)//type error
//redeclare
const firstName = 'chennai'
console.log(firstName)//syntax error

如果JavaScript是一种解释语言,因为它逐行执行代码,并在遇到错误时停止执行,那么在我的情况下,为什么类型错误没有打印在我的终端中呢?相反,它跳过并继续打印语法错误?

重新解析被认为是语法错误,一旦引擎试图解析JS代码,就会发生这种错误。TypeError是在运行时引发的,这种情况在解析后发生。

因为您将firstName变量设置为const(from constant-表示不变值(,然后尝试重新分配它。在这种特定情况下,使用let而不是const

最新更新