In Python3.8 There is new operator called walrus
which can assign new variables inside a condition. Is there something similar to it in Node.Js ?
my_var = 5
if (result := my_var == 5):
print(result)
The walrus operator creates an assignment expression. The operator allows us to assign a value to a variable inside a Python expression. It is a convenient operator which makes our code more compact. We can assign and print a variable in one go.
The walrus operator was implemented by Emily Morehouse, and made available in the first alpha release of Python 3.8.
Assignment expression are written with a new notation (:=) . This operator is often called the walrus operator as it resembles the eyes and tusks of a walrus on its side. The assignment expression allows you to assign True to walrus , and immediately print the value.
Assign and compare as one expression. To make it work in strict mode, and avoid linter complaints, add parentheses to the assignment.
const my_var = 5;
let result;
if ((result = my_var) === 5) {
console.log(result);
}
https://eslint.org/docs/rules/no-cond-assign#except-parens
There's no need for a separate operator, assignment is already an expression in Javascript:
"use strict";
var my_var = 5;
var result;
if (result = my_var == 5){
console.log(result)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With