Guest User

Untitled

a guest
Jan 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. # Shorthand variable assignment:
  2.  
  3. // A)
  4. // if all variables are being assigned equal values:
  5. // instead of this
  6. var a = 1;
  7. var b = 1;
  8. var c = 1;
  9.  
  10. // do this
  11. var a = b = c = 1;
  12. // first c is set to 1
  13. // then b is set to c as c = 1 and same goes with a
  14.  
  15. /***************************************************************************************************************************************/
  16.  
  17. // How ever this is not a recommended way to assign variables. Because we haven't declared variables b and c because of which they won't be locally scoped to the current block of code.
  18. // Both will be globally scoped and end up polluting the global namespace.
  19.  
  20. // B)
  21. // instead of
  22. var d = 2;
  23. var e = 5;
  24. var f = 8;
  25.  
  26. // do this
  27. var d = 2, e = 5, f = 8; // OR
  28. var d = 2,
  29. e = 5,
  30. f = 8;
  31.  
  32. // all three variables i.e d, e, and f are locally scoped.
Add Comment
Please, Sign In to add comment