Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript && operator as if statement? [duplicate]

This should be quite a simple question I just couldn't find the answer anywhere so thought I'd save myself some time and ask here.

I was looking at some javascript code and noticed an expression of the form:

a < b && (other maths statements);

The other maths statements are just assigning of variables and simple stuff.

My question is this. Is this another way to write compact if statements?

I know that statement?if-block:else-block; is one way to do it but as && is short-circuit and left-associative this is all I can think it would be. I know you can't have an else block with this method but it's pretty neat.

Can someone just validate I'm awake at this time in the morning please.

Thanks,

like image 921
Blue42 Avatar asked Nov 25 '13 10:11

Blue42


People also ask

Is JavaScript and HTML same?

Both of these are computer languages that help in programming, but there is a major difference between JavaScript and HTML. While JavaScript (abbreviated as JS) is a scripting language, HTML is a markup language. We use HTML to create web pages or web applications.

Which is better HTML or JavaScript?

JavaScript simply adds dynamic content to websites to make them look good. HTML work on the look of the website without the interactive effects and all. HTML pages are static which means the content cannot be changed. It adds interactivity to web pages to make them look good.

What is the JavaScript used for?

Javascript is used by programmers across the world to create dynamic and interactive web content like applications and browsers. JavaScript is so popular that it's the most used programming language in the world, used as a client-side programming language by 97.0% of all websites.

Is JavaScript same as Python?

Python is a high-level general-purpose interpreted programming language that was developed to emphasize code readability. JavaScript is a programming language that conforms to the ECMAScript specification. 2. It is a scripting language used for developing both desktop and web applications.


2 Answers

Yes, you can do it. If a is less than b the other statements are going to execute.

That said, you probably shouldn't do it. Rather than trying to save yourself a small amount of typing you should instead go with a more readable piece of code whose purpose is immediately obvious. After all, if you needed to check that you weren't misinterpreting it, other programmers who may have to read or modify your code could have the same problem.

There is also a pitfall with this if you were trying to do multiple assignments, but one of the middle assignments was for a "falsey" value. Consider this example:

a < b && (a = 1) && (b = 0) && (c = 3);

Essentially, if a is less than b, set a to 1, b to 0 and c to 3. However, the (b = 0) returns false (0 is considered false when converted to a boolean), so the (c = 3) part never executes. Obviously with that basic example anybody who is reasonably knowledgeable about JavaScript could work out that it will fail, but if the values for a, b and c were coming from elsewhere there'd be no immediate indication that it wouldn't always work.

like image 120
Anthony Grist Avatar answered Sep 27 '22 22:09

Anthony Grist


You can use this type of if-else statement for shorter code, although in my opinion it's less readable:

JavaScript:

var el = document.getElementById('test'),
a = 1,
b = 0;

a>b && (el.innerHTML = "True") || (el.innerHTML = "False");

HTML:

<div id="test"></div>

An example jsFiddle

If you switch the operator from greater-than to less-than, you will see "False" printed inside of the "test" DIV.

like image 20
Trendy Avatar answered Sep 27 '22 23:09

Trendy