var text = 'abc'; if(text = '' || text != '') console.log(text); else console.log('in else');
This is just a useless piece of code, but it gives a strange result that I did not expect at all. Therefore, my curiosity brought me here.
It prints only true.
true
Why does it update the value textto true, and not set it to empty?
text
Expression
text = '' || text != ''
analyzed as
text = ('' || text != '')
Value
('' || text != '')
- this is a logical meaning true, because text != ''- true.
text != ''
The condition '' || text != ''is evaluated as true. and this value is then assigned to the variable text.
'' || text != ''
'' - , || (OR). text , text != '' true, text.
''
||
Source: https://habr.com/ru/post/1611972/More articles:Unable to pass authentication information to NetSuite REST interface using Python - pythonЦиркулярные предупреждения и ошибки CS1703 в Microsoft.CSharp, System.Collections, System.Threading, внутри-Framework-конфликт - c#Javascript: innerHTML object that does not populate the parent - javascriptMongodb Async vs Sync driver for Java - javaHow can I avoid the guice conical tile code - guice"HttpClientFactory" does not exist in the current context - asp.net-mvcPrint syms / matlabFunction slow - matlabAutomata Regular Expression - The Difference Between Concatenation and Combining - regexHow to skip empty files when importing text files into R? - for-loopUse your own model in a train (cardboard packaging)? - rAll Articles