JS switch


switch statement selects the matched case out of multiple conditions to execute the specific codes.

x=2;
switch (x)
{
	case 1:
		x += 5;
		alert(x);
		break;
	case 2:
		x += 10;
		alert(x); //12
		break;
	case 3:
		x += 15;
		alert(x);
		break;
	default:
		x += 20;
		alert(x);
		break;
}


The break statement stops all statements after the matched case. If no break statement, all scripts after the matched case will be executed.

x=2;
switch (x)
{
	case 1:
		x += 5;
		alert(x);
	case 2:
		x += 10;
		alert(x); //12
	case 3:
		x += 15;
		alert(x); //27
	default:
		x += 20;
		alert(x); //47
}




endmemo.com © 2024  | Terms of Use | Privacy | Home