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

10

172

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?

Lunette answered 28/10, 2015 at 5:53 Comment(4)
Isn't {} empty braces the same thing ?Stopgap
@Stopgap that is trueFloorage
null is sometimes used for that, as in if (condition) null;Coy
You literally don't need one. If you want, just add a comment. But really, in JavaScript you just leave it completely empty.Dodge
D
247

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. {}.

Dynah answered 28/10, 2015 at 5:55 Comment(0)
C
80

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

Cass answered 3/11, 2017 at 9:8 Comment(2)
when newlines are stripped incorrectly from HTML // double slash comments get to comment everything after it. its better to use /* */ than slashSliver
nice solution, right here. KISSNomo
P
22

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
}
Phenolphthalein answered 28/10, 2015 at 5:58 Comment(0)
S
6

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.

Strategist answered 8/4, 2020 at 23:50 Comment(1)
This works exactly the same way in Python, you can't use pass in a conditional expression, you would have to use a conditional statementPentylenetetrazol
O
6

you could create a function that actually does nothing.

const pass = () => {}
try {
  pass()
} else {
  console.log('helloworld!')
}
Overcurious answered 27/1, 2021 at 13:32 Comment(0)
F
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!')
Fula answered 17/1, 2021 at 10:55 Comment(0)
B
3

In some cases pass can just be ;

A real life example can be:

var j;
for (j = i + 1; j < binstrN.length && binstrN[j] != 1; j++) {
}
let count = j - i;

is same as

var j;
for (j = i + 1; j < binstrN.length && binstrN[j] != 1; j++);
let count = j - i;

Here we are trying to move j to next '1', while i was already at a '1' before it, hence count gives the distance between first two '1's in the string binary string binstrN

Bitthia answered 14/9, 2020 at 7:1 Comment(0)
N
3

An easy way for passing an if statement is entering a string. 'pass' is more readible.

if(true){
'pass'
}
Narcissus answered 27/9, 2021 at 20:57 Comment(0)
G
2

If you want to just use the pass operator in a ternary operator or just in an if statement in JS, you can do this:

a === true && (console.log('okay'))

You can also use the opposite of && which is the || operator. Then if you want to use Pass in a function or a block in general as we do in Python like this:

def Func(): pass

In JS if it's an if/else statement, you don't need to have that block at all that would pass, you just don't add it:

if(condition) {
  console.log('ok')
}
/* else {} remove it! /

And if it's a function that you are planning to implement later, you can throw an error inside it, that way if you use it, you get a nice error that reminds you to implement it.

function getUser() {
 throw "Not Implemented Yet";
}

In the end, there are no braces in Python, which is why we have a pass.

Gerome answered 26/10, 2020 at 17:28 Comment(0)
S
0

I've found that I get an error with empty braces, instead I put a semicolon in there, basically the same thing:

try { //something; } catch (err) { ; }
Selfeffacement answered 27/5, 2016 at 20:54 Comment(2)
What error do you get with empty braces with no semicolon inside?Jeep
when newlines are stripped incorrectly from HTML // double slash comments get to comment everything after it. its better to use /* */ than slash like thatSliver

© 2022 - 2024 — McMap. All rights reserved.