conditional operator - Will `if(x=y)` Ever Return false, or fail In JavaScript? - Stack Overflow
This is a theoretical question, as I can't imagine any practical uses.
I made a bold statement today saying that in JavaScript, the following will always return true:
if (x=y){
//code
}
And the //code
, whatever it is, will always be executed.
This is the classic typo of not entering ==
or even ===
.
This feature can also be demonstrated in C/C++, but being more strongly-typed languages than JavaScript, it is not hard to think instances where this assignment will fail.
However, in JavaScript, given two variables x
and y
, I was struggling to think of an occation where this would fail, or the proceding conditional code block would not execute.
Anyone?
This is a theoretical question, as I can't imagine any practical uses.
I made a bold statement today saying that in JavaScript, the following will always return true:
if (x=y){
//code
}
And the //code
, whatever it is, will always be executed.
This is the classic typo of not entering ==
or even ===
.
This feature can also be demonstrated in C/C++, but being more strongly-typed languages than JavaScript, it is not hard to think instances where this assignment will fail.
However, in JavaScript, given two variables x
and y
, I was struggling to think of an occation where this would fail, or the proceding conditional code block would not execute.
Anyone?
Share Improve this question asked Nov 4, 2010 at 8:55 Mutation PersonMutation Person 30.5k18 gold badges100 silver badges165 bronze badges 1- For the downvoter: If you ever return, please explain why. Downvotes without explanation are pretty useless. – Mutation Person Commented Nov 4, 2010 at 9:35
3 Answers
Reset to default 14It (x=y) would evaluate to false if y=0, y=null, y=undefined or y=false.
Edit: Also if y=NaN
Edit: Also if y=""
The conditional block "x=y" will always execute. But in javascript "false", undefined, null, and 0 evaluate to false. So whenever y is one of those values, the "//code" will not be executed.
js
js> if(x=y){
print('hello');
}
typein:1: ReferenceError: y is not defined
js>
- SaaS 的历史与变革
- 谷歌继续封死华为后路,Mate 30无法安装谷歌服务
- 苹果允许预装软件可卸载 或因遭受安卓手机阵型冲击
- 有点厉害 win10兼容安卓应用方式曝光
- 软件主宰世界 如何统治电脑业、数字传媒等行业
- 谷歌正在复制苹果模式?(图)
- Intel毫不客气:ARM+Win8软硬件都不行
- 分析:Windows 8平板电脑称雄市场需三大因素
- Azure web app High availability with DNS failover - Stack Overflow
- How to Interact with iframe in Google Form “add file” feature Using Python and Playwright? - Stack Overflow
- mvvm - How to Call ViewModelProvider without ref in Flutter - Stack Overflow
- reactjs - Scrollable Panels Not Working in React-Resizable-Panels - Stack Overflow
- javascript - PHP Cross-Domain POST Request - Session Cookie Not Persisting After Redirect - Stack Overflow
- postgresql - How to divide two NUMERIC values and get a result that has the maximum number of digits the NUMERIC type supports?
- python - Azure Cognitive Vector search query and index creation - Stack Overflow
- sql - How to validate data is Hexadecimal in Presto - Stack Overflow
- macos - Image from ImagePicker in landscape - Stack Overflow