简体   繁体   中英

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

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?

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

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

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
}

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.

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!')

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

you could create a function that actually does nothing.

const pass = () => {}
try {
  pass()
} else {
  console.log('helloworld!')
}

我发现我在使用空大括号时出现错误,而是在那里放了一个分号,基本上是一样的:

try { //something; } catch (err) { ; }

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 use also use || operator but you should know that the || is the opposite of &&. 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 you should just leave the block empty as this:

 if(){ 
    console.log('ok')
    }else{}

In the end, there are no braces in Python, so this is the main reason why we have a pass.

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

 if(true){ 'pass' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM