Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing switch cases

How can I do something like this with a switch statement:

String.prototype.startsWith = function( str ){
    return ( this.indexOf( str ) === 0 );
}

switch( myVar ) {
    case myVar.startsWith( 'product' ):
        // do something 
        break;
}

This is the equivalent of:

if ( myVar.startsWith( 'product' )) {}
like image 268
HyderA Avatar asked Jun 10 '26 14:06

HyderA


1 Answers

You can do it, but it's not a logical use of the switch command:

String.prototype.startsWith = function( str ){
    return ( this.indexOf( str ) === 0 );
};

var myVar = 'product 42';

switch (true) {
    case myVar.startsWith( 'product' ):
        alert(1); // do something
        break;
}
like image 121
Guffa Avatar answered Jun 12 '26 11:06

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!