Javascript switch case example

How to make a switch case in javascript?

 The switch statement is the same as if else condition. Switch case decided by the user we can use only Numeric value Or character value. Also, we use here a single expression for more choices.
switch case example below:

Example:
<input type="text" id="vegtValue"><input type="submit" value="Submit" onclick="vegtMain()">
   <br>
   <div class="note">Note: After click on the submit button, check on the console. </div>
   <br><br>
<div><strong>Search keyword: </strong>Radish, Carrot, Turnip, Potato, Colocasia</div>
    <script>
    function vegtMain(){
        var vegValue = document.getElementById('vegtValue').value;
       // console.log(vegValue);
        switch(vegValue){
            case 'Radish':
            console.log('Radish rate 20 rupee/kg');
            break;
            case 'Carrot':
            console.log('Carrot rate 40 rupee/kg');
            break;
            case 'Turnip':
            console.log('Turnip rate 30 rupee/kg');
            break;
            case 'Potato':
            console.log('Potato rate 15 rupee/kg');
            break;
            case 'Colocasia':
            console.log('Colocasia rate 50 rupee/kg');
            break;
            default:
            console.log('Vegtable not available now.');
        }
    }
    </script>

No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.