如果在 JavaScript 中的 if 条件之后在执行块中使用两个等于而不是只有一个,会发生什么情况

what would happen if you use two equals rather than only one in your execution block after an if conditional in JavaScript?

本文关键字:两个 有一个 什么情况 条件 之后 if 中的 JavaScript 执行 如果      更新时间:2023-09-26

我还在学习,所以如果我的问题格式不好,我很抱歉。

试图编写一个函数来将单词插入到指定位置的字符串中,但是我犯了一个错误,即在执行块中==写两个等号而不是一个等号,这导致测试时输出错误。

但是,我已经知道 if/else 之后的代码执行不需要是布尔值,我注意到了这个错别字,我通过删除一个等号来纠正它,函数工作得很好,但我只是想知道为什么我从来没有质疑在 if 条件之后执行代码时严格拥有一个等号的重要性。

所以这是错误的代码:

function insert(original,to_insert,position){ 
    if (to_insert == "undefined" && position == undefined){return original;}
    else if (position == undefined){position == 0}
    var x = original.slice(0,position); 
    var y = original.slice(position); 
    console.log(x + to_insert + y);
}
insert('We are doing some exercises.','JavaScript '); 
//Wrong output >>>> "We are doing some exercises.JavaScript We are doing some exercises."
insert('We are doing some exercises.','JavaScript ', 18);
//correct output>>>>  "We are doing some JavaScript exercises."

这是正确的代码:

function insert(original,to_insert,position){ 
    if (to_insert == "undefined" && position == undefined){return original;}
    else if (position == undefined){position = 0}
    var x = original.slice(0,position); 
    var y = original.slice(position); 
    console.log(x + to_insert + y);
}
insert('We are doing some exercises.','JavaScript '); 
//correct output >>>> JavaScript We are doing some exercises."
insert('We are doing some exercises.','JavaScript ', 18);
//correct output>>>>  "We are doing some JavaScript exercises."

您能否解释一下我的错误代码中发生了什么,例如使用布尔值时导致函数无法正常运行的原因,显然该函数一次运行一次,那么位置的绝对值与位置的变量值相比会有什么区别。

提前致谢

else if (position == undefined){position == 0}

在你的错误代码中,position仍然undefined因为你没有做作业,你只是检查positionundefined)是否等于0

因此,当您这样做时var x = original.slice(0,position); slice()只是忽略了第二个参数,在这种情况下,第二个参数是undefined并从头到尾切片,这是不使用第二个参数时的默认行为。

来自 MDN:

slice() 方法提取字符串的一部分并返回一个新字符串。

str.slice(beginSlice[, endSlice])

结束切片

自选。结束提取的从零开始的索引。如果省略,slice() 将提取到字符串的末尾。如果为负数,则将其视为 sourceLength + endSlice,其中 sourceLength 是字符串的长度(例如,如果 endSlice 为 -3,则将其视为 sourceLength - 3)。

在您的情况下,由于您传递了undefined(因为position == undefined),就像您省略了它一样

一个等于是给变量赋值,两个等于是给两个变量赋值。这是最简单的解释方式。

= - 赋值运算符== - 比较运算符

if (position == undefined){position == 0}

这意味着如果你的仓位是未定义的仓位,则必须为 0。就像它应该是 0 但你没有定义它。两个等于通常用来做比较does position is equals to 0

但是,一个相等意味着您将值 0 分配给位置。

我可以在你的代码中看到两个问题。

if (to_insert == "undefined" && position == undefined){return original;}

在这里,您检查to_insert是否等于字符串"undefined",但不等于undefined

else if (position == undefined){position == 0}

写入position == 0只会返回一个布尔值。所以在这种情况下,它将返回false(因为它仅在返回 true 时才执行position == undefined)。

因此,这就像在您的代码中,您在两行之间有一个false,并且您不会更改任何变量的值。

else if (position == undefined){position = 0}

通过只写入一个=,可以将值0分配给变量position

因此,当您调用 slice() 方法时,第二个参数仍然是undefined,因此该方法会忽略它,只是将字符串切到最后。

希望我能帮你理解!

你实际上是在问,"使用一个等号 (=) 和使用两个等号 (==) 有什么区别?

假设我们对这两个示例都有以下初始化:

var pikachu_hp;

一个等号 (=):

pikachu_hp = 50;

这会将变量 pikachu_hp 设置为具有值为 50"数字"数据类型。

两个等号 (==):

pikachu_hp == 60;

这将pikachu_hp的值(不是数据类型,在 JavaScript 中是三个 (===) 等号)与右侧的值进行比较;在本例中,这是数字 60。如果pikachu_hp的数据值为 60,则表达式返回 true 。如果 pikachu_hp 的数据值不是 60,则表达式返回 false 。同样,我称之为"表达",因为它不等同于任何东西;它表示truefalse值。