Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
Is it legal to have expressions not assigned to values in Javascript functions?

I'm sure this is a duplicate but I cannot find the original question, so please link to it if you know of it.

In Javascript I am currently writing a function and I am tempted to write it like this:

function toggleDropdown() {
  isOpen() ? closeDropdown() : openDropdown(); 
}

However, this feels a bit strange, as the ternary operator is an expression, and neither closeDropdown or openDropdown return values (save for an implicit undefined), and I am not assigning the return value of the ternary to anything.

This I realized that Javascript seems to allow you to just arbitrarily calculate and have expressions not assigned to anything. For example, apparently this code below doesn't throw any errors:

function test() {
  55;
  "asdf";
  let x = 12;
  "qwer";
  12;
}

Which seems kind of strange to me? I'm wondering, is this code "okay" to write? It doesn't seem to throw any errors, but I'm worried that it will break during some sort of compilation step with Babel or something, as this code seems a bit unorthodox. Or maybe it is completely supported?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Its all good. You do not have to do anything with the result of an expression.

It is really no different than calling a function that returns a value, and not assigning or using the value.

In the case of the primative values, they would most likely just get optimized out if you were optimizing your code.

With that said, you may have a linter that is configured to actually emit a warning or an error for an unassigned expression, so your valid syntax would be shown as an issue in that case.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!