0

In the Mozilla Developers Guide on how to write a WebSocket Server in c# I found this two lines of code in the js client part.
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server

var text = textarea.value;
text && doSend(text);

Can somebody tell me, why they would put the var "text" in front of && before the function call?

Andiroid
  • 3
  • 1

1 Answers1

0

It's a shorthand for

if (text) {
  doSend(text);
}

If text is falsy (null or undefined or an empty string) then the right part will not be executed.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • I see thanks. I also wasn't sure which cases of nullity were supported.. – Andiroid May 28 '20 at 11:11
  • @Andiroid That's the common 'truthy/falsy' feature. Javascript understands that it needs to boolean values for the `&&` operation and 'converts' `text` into one. See this one: https://developer.mozilla.org/en-US/docs/Glossary/Truthy – Andreas Dolk May 28 '20 at 14:54
  • thank you very much, exactly what I was looking for. – Andiroid May 28 '20 at 15:16