发布于 2026-01-06 8 阅读
0

了解这些JS概念之间的区别,提升你的技能 #1

了解这些JS概念之间的区别,提升你的技能 #1

嘿!欢迎来到这里!读完本文,你将了解许多开发人员经常问到的概念之间的区别,从而提高你的 JavaScript 技能!

展开运算符与剩余运算符

运算符相同(...),但用法不同。

spread operator是用于参数函数的,它允许我们为该函数设置无限数量的参数!

// rest parameter is handle as array in the function
const add = (...rest) => {
   return rest.reduce((total, current) => total + current)
}

// Nice your function can handle different number of parameters !
add(1, 2, 3)
add(1, 2, 3, 4, 5)
Enter fullscreen mode Exit fullscreen mode

它还有另一种用途,例如,它允许从或中rest operator提取值:arrayobject

// We extract the first Item of the array into the variable and the others variable in an array named others
const [ firstItem, ...others ] = [ 1, 2, 3, 4 ]
firstItem // 1
others // [ 2, 3, 4 ]
Enter fullscreen mode Exit fullscreen mode

我们可以用物体来制作它

const toto = { a: 1, b: 2, c: 3 }
const { a, ...others } = toto
a // 1, we extract the a key from toto object
others // { b: 2, c: 3 }, we extract other key in the object thanks to rest operator 
Enter fullscreen mode Exit fullscreen mode

空值合并(??)与 OR 运算符(||

乍一看,我们可能会认为空合并运算??符()类似于 OR||运算符()。但实际上并非如此!

它将Nullish检查该值是否为nullundefined

和 ...

运算OR符将检查该值是否falsy为真。

还是不明白?让我们看看下面的代码!

const foo = 0 ?? 42
const foo2 = 0 || 42

foo // 0 since nullish check if value is null or undefined
foo2 // 42 since OR operator will check if the value is falsy, in this example it's the case

const toto = null ?? 42
const toto2 = null || 42

toto // 42 since nullish check if value is null or undefined
toto2 // 42 since OR operator will check if the value is falsy, in this example it's the case (null is a falsy value)
Enter fullscreen mode Exit fullscreen mode

双等号 ( ==) 与三等号 ( ===)

double equals检查值而不是类型,但如果类型不同,它会进行转换implicit coercion,以便将值转换为相同的类型并检查该值。

这个机制不太容易理解,但你可以在我之前的文章https://dev.to/codeozz/implicit-coercion-in-javascript-nehimplicit coercion中查看详细信息。

triple equals会检查值和类型!而且不会像 double equals 那样进行隐式强制转换!

一般来说,我建议你始终使用三个等号!

以下代码将说明其中的区别:

双重等于示例:

// Not the same type so implicit coercion will be made
'24' == 24

// Convert string into number so 
Number('24') == 24

// We got an number so we can check value
24 == 24 // true !
Enter fullscreen mode Exit fullscreen mode

三重等于示例:

// Not the same type and triple equal will not make an implicit coercion !
'24' === 24 // false
Enter fullscreen mode Exit fullscreen mode

var、let 和 const

面试中经常被问到的问题!

我将把这个解释分为3个部分:

I) 范围

var声明可以是全局作用域或函数作用域。

if (true) {
  var timeVar = 56
}
console.log(timeVar) // 56
Enter fullscreen mode Exit fullscreen mode

这样做很危险,因为你可能会删除一个已存在的变量。

var timeVar = 24
if (true) {
  // the first variable is erased
  var timeVar = 56
  console.log(timeVar) // 56
}
console.log(timeVar) // 56
Enter fullscreen mode Exit fullscreen mode

let&const声明的作用域是块级的。

if (true) {
  let timeLet = 56
  const timeConst = 98
  console.log(timeLet) // 56
  console.log(timeConst) // 98
}
console.log(timeLet) // Error: timeLet is not defined in this scope since it's only block scope

console.log(timeConst) // Error: timeConst is not defined in this scope since it's only block scope
Enter fullscreen mode Exit fullscreen mode

二)重新声明和更新变量

var 可以重新申报和更新

// can be re-declared
var timeVar = 56
var timeVar = 'toto'

// can be updated
timeVar = 'tutu'
Enter fullscreen mode Exit fullscreen mode

let 不能重新声明和更新

// can't be re-declared
let timeLet = 56
let timeLet = 'toto' // Error: Identifier 'timeLet' has already been declared

// can be updated
timeLet = 'tutu'
Enter fullscreen mode Exit fullscreen mode

const 不能重新声明,也不能更新

// can't be re-declared
const timeConst = 56
const timeConst = 'toto' // Error: Identifier 'timeConst' has already been declared

// can't be updated
timeConst = 'tutu' // TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

三)吊装

有些人不了解HoistingJavaScript 中的提升句柄,我不会在本文中解释它是什么,但var一种不同的方法。letconst

// They are all hoisted to the to of their scope.
// But while var variable are initialized with undefined
// let and const variables are not initialized.

myVarVariable // undefined
myLetVariable // Error since not initialized by Hoisting, you cannot use it's declaration
myConstVariable // Error since not initialized by Hoisting, you cannot use it's declaration

myVarVariable = 5
myLetVariable = 'letValue'
myConstVariable = 'constValue'

var myVarVariable
let myLetVariable
const myConstVariable
Enter fullscreen mode Exit fullscreen mode

一般来说,我建议您始终使用const(表示常量值),let对于其他目的则使用。


希望您喜欢这篇文章!

🎁 如果你在推特上关注我并私信我,就可以Underrated skills in javascript, make the difference免费获得我的新书哦😁

或者点击这里获取。

🎁我的电子报

☕️ 您可以支持我的作品🙏

🏃‍♂️ 你可以关注我 👇

🕊 Twitter:https://twitter.com/code__oz

👨‍💻 Github:https://github.com/Code-Oz

您可以收藏这篇文章!

文章来源:https://dev.to/codeoz/know-the-difference- Between-theses-js-concept-in-order-to-skill-up-1-4317