Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here are additional answers to some likely questions:</p> <p>JavaScript only has two scopes:</p> <ol> <li><p>Global Scope</p></li> <li><p>Function Scope</p></li> </ol> <p>There is <strong>no block scope</strong> in JavaScript!</p> <p>All variables declared inside a scope are available as if they were all declared at the begin of that scope, due to <strong>variable hoisting</strong>.</p> <p>This is why it is recommended to declare all variable at the begin of their scope, to better reflect reality, possibly in a single var declaration.</p> <p>e.g.</p> <pre><code>function person(firstname, lastname, age, eyecolor) { var changeName = function (name) { this.lastname = name; } this.firstname = firstname; this.lastname = lastname; this.age = age; this.eyecolor = eyecolor; this.changeName = changeName; } </code></pre> <p>Actually, in this case, the variable or named function is not needed at all.</p> <p>Just assign an anonymous function to the object property directly:</p> <pre><code>function person(firstname, lastname, age, eyecolor) { this.firstname = firstname; this.lastname = lastname; this.age = age; this.eyecolor = eyecolor; this.changeName = function (name) { this.lastname = name; } } </code></pre> <p>Tools like JSLint or JSHint will help you improve your JavaScript code.</p> <p>See <a href="http://codepen.io/anon/pen/bEJFn" rel="nofollow">this sample @ codepen.io</a> (press CTRL+SHIFT+7 in the JS Editor) and <a href="http://blog.codepen.io/2013/06/11/inline-error-messaging-for-js-hint/" rel="nofollow">JSHint use at codepen.io</a>.</p> <p>Finally, the Mozilla Developer Network is a much better resource than W3Schools.</p> <p>It will even teach you in the <a href="https://developer.mozilla.org/en-US/docs/Web/API/document.write#Notes" rel="nofollow">document.write Notes</a> that is not good practice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload