ES 2015 — New Block-Level Variable Declarations
In contrast to other curly brace programming languages like C, C++, Java, or C#, JavaScript shows some unusual behavior. Take a look at this code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Exercise-01-01.js function getWiggin(childIndex) { if (childIndex === 1) { var wiggin = 'Peter'; } else if (childIndex === 2) { var wiggin = 'Valentine'; } else if (childIndex === 3) { wiggin = 'Ender'; } return wiggin; } console.log("The First:", getWiggin(1)); console.log("The Second:", getWiggin(2)); console.log("The Third:", getWiggin(3)); console.log("Unknown:", getWiggin(4)); |
The multiple declarations of the wiggin variable in getWiggin suggests that the instance within the childIndex === 1 branch is different from the other created in the childIndex === 2 section….