basics
BLOCK
The phone store employee must go through a series of steps to complete the checkout as
you buy your new phone.
Similarly, in code we often need to group a series of statements together, which we often call
a block. In JavaScript, a block is defined by wrapping one or more statements inside a curlybrace
pair { .. } . Consider:
var amount = 99.99;
// a general block
{
amount = amount * 2;
console.log( amount ); // 199.98
}
This kind of standalone { .. } general block is valid, but isn't as commonly seen in JS
programs. Typically, blocks are attached to some other control statement, such as an if
statement (see "Conditionals") or a loop (see "Loops"). For example:
var amount = 99.99;
// is amount big enough?
if (amount > 10) { // <-- block attached to `if`
amount = amount * 2;
console.log( amount ); // 199.98
}
We'll explain if statements in the next section, but as you can see, the { .. } block with
its two statements is attached to if (amount > 10) ; the statements inside the block will only
be processed if the conditional passes.
Note: Unlike most other statements like console.log(amount); , a block statement does not
need a semicolon ( ; ) to conclude it.
SCOPE
function outer() {
var a = 1;
function inner() {
var b = 2;
// we can access both `a` and `b` here
console.log( a + b ); // 3
}
inner();
// we can only access `a` here
console.log( a ); // 1
}
outer();
Lexical scope rules say that code in one scope can access variables of either that scope or
any scope outside of it.
So, code inside the inner() function has access to both variables a and b , but code in
outer() has access only to a -- it cannot access b because that variable is only inside
inner() .
Coercion
Coercion comes in two forms in JavaScript: explicit and implicit. Explicit coercion is simply
that you can see obviously from the code that a conversion from one type to another will
occur, whereas implicit coercion is when the type conversion can happen as more of a nonobvious
side effect of some other operation.
Here's an example of explicit coercion:
var a = "42";
var b = Number( a );
a; // "42"
b; // 42 -- the number!
And here's an example of implicit coercion:
Into JavaScript
42
var a = "42";
var b = a * 1; // "42" implicitly coerced to 42 here
a; // "42"
b; // 42 -- the number!
The phone store employee must go through a series of steps to complete the checkout as
you buy your new phone.
Similarly, in code we often need to group a series of statements together, which we often call
a block. In JavaScript, a block is defined by wrapping one or more statements inside a curlybrace
pair { .. } . Consider:
var amount = 99.99;
// a general block
{
amount = amount * 2;
console.log( amount ); // 199.98
}
This kind of standalone { .. } general block is valid, but isn't as commonly seen in JS
programs. Typically, blocks are attached to some other control statement, such as an if
statement (see "Conditionals") or a loop (see "Loops"). For example:
var amount = 99.99;
// is amount big enough?
if (amount > 10) { // <-- block attached to `if`
amount = amount * 2;
console.log( amount ); // 199.98
}
We'll explain if statements in the next section, but as you can see, the { .. } block with
its two statements is attached to if (amount > 10) ; the statements inside the block will only
be processed if the conditional passes.
Note: Unlike most other statements like console.log(amount); , a block statement does not
need a semicolon ( ; ) to conclude it.
SCOPE
function outer() {
var a = 1;
function inner() {
var b = 2;
// we can access both `a` and `b` here
console.log( a + b ); // 3
}
inner();
// we can only access `a` here
console.log( a ); // 1
}
outer();
Lexical scope rules say that code in one scope can access variables of either that scope or
any scope outside of it.
So, code inside the inner() function has access to both variables a and b , but code in
outer() has access only to a -- it cannot access b because that variable is only inside
inner() .
Coercion
Coercion comes in two forms in JavaScript: explicit and implicit. Explicit coercion is simply
that you can see obviously from the code that a conversion from one type to another will
occur, whereas implicit coercion is when the type conversion can happen as more of a nonobvious
side effect of some other operation.
Here's an example of explicit coercion:
var a = "42";
var b = Number( a );
a; // "42"
b; // 42 -- the number!
And here's an example of implicit coercion:
Into JavaScript
42
var a = "42";
var b = a * 1; // "42" implicitly coerced to 42 here
a; // "42"
b; // 42 -- the number!
Comments
Post a Comment