Skip to content

Is the binding visible in the else? #3

@bakkot

Description

@bakkot

Moving from #2.

Consider

if (const a = Math.random() < .5) {
  console.log(a); // definitely works
  console.log(b); // definitely throws
} else if (const b = Math.random() < .5) {
  console.log(a); // works?
  console.log(b); // definitely works
} else {
  console.log(a); // works?
  console.log(b); // works?
}

What should the three works? lines do?


For background, in C++,

#include <iostream>
using namespace std;

int main() {
	if (bool a = false) {
		cout << a << endl;
	} else if (bool b = false) {
		cout << a << endl;
		cout << b << endl;
	} else {
		cout << a << endl;
		cout << b << endl;
	}
	return 0;
}

is a legal program.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions