Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. // This example shows that in javascript even
  2. // the case expressions in the switch is dynamic
  3.  
  4. 'use strict';
  5.  
  6. let prefix = 'label';
  7.  
  8. function getLabel(n) { return prefix + n };
  9.  
  10. console.log(getLabel(1)); // prints "label1"
  11.  
  12. function print(label){
  13. switch(label){
  14. case getLabel(1): console.log(1); break;
  15. case getLabel(2): console.log(2); break;
  16. default: console.log("HELP ME!!!");
  17. }
  18. }
  19.  
  20. print('label2'); // prints 2
  21.  
  22. prefix = 'cow';
  23.  
  24. print('label2'); // prints "HELP ME!!!"
  25.  
  26. print('cow2'); // prints 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement