Is there a JavaScript equivalent of the Python pass statement that does nothing?

103,982

Solution 1

Python's pass mainly exists because in Python whitespace matters within a block. In Javascript, the equivalent would be putting nothing within the block, i.e. {}.

Solution 2

use //pass like python's pass

like:

if(condition){
   //pass
}

This is equivalent to leaving the block with nothing in it, but is good for readability reasons.

reference from https://eslint.org/docs/rules/no-empty

Solution 3

python's pass is required for empty blocks.

try:
    # something
except Exception:
    pass

In javascript you can simply catch an empty block

try {
    // some code
} catch (e) {
    // This here can be empty
}

Solution 4

Javascript does not have a python pass equivalent, unfortunately.

For example, it is not possible in javascript to do something like this:

process.env.DEV ? console.log('Connected..') : pass

Instead, we must do this:

if (process.env.DEV) console.log('Connected..')

The advantage of using the pass statement, among others, is that in the course of the development process we can evolve from the above ternary operator example in this case without having to turn it into a full if statement.

Solution 5

I know this is a very old question but i guess that is also possible to do something like this.
You can declare a constant that contains a string (or something else).

const pass = 'pass';

const pass = null; may also be good.

if (condition) {
   pass
} else {
   console.log('hi!');
}

However note also that this may be a better option.

if (condition) {}
else {
    console.log('cool!');
}

Python doesn't have brackets to determine where the blocks of code are like javascript, so an empty block throws error (that's why you put the pass statement in empty blocks). What i have done by answering this question is just creating a constant using it as if it was a statement. The concept is really near to python's substitution of pass with ellipsis.
Someone in python prefers to use ... instead of pass.

if condition:
    ...
else:
    print('Python!')
Share:
103,982
guagay_wk
Author by

guagay_wk

Updated on September 28, 2021

Comments

  • guagay_wk
    guagay_wk over 2 years

    I am looking for a JavaScript equivalent of the Python:

    pass statement that does not run the function of the ... notation?

    Is there such a thing in JavaScript?

    • sagarchalise
      sagarchalise over 8 years
      Isn't {} empty braces the same thing ?
    • cobrexus
      cobrexus about 4 years
      @sagarchalise that is true
    • Parzh from Ukraine
      Parzh from Ukraine about 4 years
      null is sometimes used for that, as in if (condition) null;
    • Jean Ravenclaw
      Jean Ravenclaw about 3 years
      You literally don't need one. If you want, just add a comment. But really, in JavaScript you just leave it completely empty.
  • Admin
    Admin about 7 years
    What error do you get with empty braces with no semicolon inside?
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 4 years
    This works exactly the same way in Python, you can't use pass in a conditional expression, you would have to use a conditional statement
  • fedmich
    fedmich over 3 years
    when newlines are stripped incorrectly from HTML // double slash comments get to comment everything after it. its better to use /* */ than slash
  • fedmich
    fedmich over 3 years
    when newlines are stripped incorrectly from HTML // double slash comments get to comment everything after it. its better to use /* */ than slash like that