[Solved] Variable assignment in if statement


There’s no such equivalent solution in JavaScript, because there’s no equivalent problem.

What you’re demonstrating is conditional binding, which exists to allow you to create a non-optional value (x, in your example), out of an optional value (y). There’s no such need in Javascript, because all values are nullable, for better or for worse.

You just do a null check, like so:

if (y) {

}

Just as how you shouldn’t write JavaScript code in Swift, you shouldn’t write Swift code in JavaScript. It’s a completely different languages, with completely different features, syntax, patterns and designs.

solved Variable assignment in if statement