官术网_书友最值得收藏!

Code blocks

In the preceding examples, you saw the use of code blocks. Let's take a moment to clarify what a block of code is, because you use blocks extensively when constructing conditions and loops.

A block of code consists of zero or more expressions enclosed in curly brackets:

{
  var a = 1;
  var b = 3;
}

You can nest blocks within each other indefinitely:

{
  var a = 1;
  var b = 3;
  var c, d;
  {
    c = a + b;
    {
      d = a - b;
    }
  }
}
Tip

Best practice tips

  • Use end-of-line semicolons, as discussed previously in the chapter. Although the semicolon is optional when you have only one expression per line, it's good to develop the habit of using them. For best readability, the individual expressions inside a block should be placed one per line and separated by semicolons.
  • Indent any code placed within curly brackets. Some programmers like one tab indentation, some use four spaces, and some use two spaces. It really doesn't matter, as long as you're consistent. In the preceding example, the outer block is indented with two spaces, the code in the first nested block is indented with four spaces, and the innermost block is indented with six spaces.
  • Use curly brackets. When a block consists of only one expression, the curly brackets are optional, but for readability and maintainability, you should get into the habit of always using them, even when they're optional.

Checking if a variable exists

Let's apply the new knowledge about conditions for something practical. It's often necessary to check whether a variable exists. The laziest way to do this is to simply put the variable in the condition part of the if, for example, if (somevar) {...}. But, this is not necessarily the best method. Let's take a look at an example that tests whether a variable called somevar exists, and if so, sets the result variable to yes:

> var result = '';
> if (somevar) { 
    result = 'yes'; 
  }
ReferenceError: somevar is not defined
> result;
""

This code obviously works because the end result was not "yes". But firstly, the code generated an error: somevar is not defined, and you don't want your code to behave like that. Secondly, just because if (somevar) returns false doesn't mean that somevar is not defined. It could be that somevar is defined and initialized but contains a falsy value like false or 0.

A better way to check if a variable is defined is to use typeof:

> var result = "";
> if (typeof somevar !== "undefined") { 
    result = "yes"; 
  }
> result;
""

typeof always returns a string, and you can compare this string with the string "undefined". Note that the variable somevar may have been declared but not assigned a value yet, and you'll still get the same result. So, when testing with typeof like this, you're really testing whether the variable has any value other than the value undefined:

> var somevar;
> if (typeof somevar !== "undefined") { 
    result = "yes"; 
  }
> result;
""
> somevar = undefined;
> if (typeof somevar !== "undefined") { 
    result = "yes"; 
  }
> result;
""

If a variable is defined and initialized with any value other than undefined, its type returned by typeof is no longer "undefined":

> somevar = 123;
> if (typeof somevar !== "undefined") { 
    result = 'yes'; 
  }
> result;
"yes"

Alternative if syntax

When you have a simple condition, you can consider using an alternative if syntax. Take a look at this:

var a = 1;
var result = '';
if (a === 1) {
  result = "a is one";
} else {
  result = "a is not one";
}

You can also write this as:

> var a = 1;
> var result = (a === 1) ? "a is one" : "a is not one";

You should only use this syntax for simple conditions. Be careful not to abuse it, as it can easily make your code unreadable. Here's an example.

Let's say you want to make sure a number is within a certain range, say between 50 and 100:

> var a = 123;
> a = a > 100 ? 100 : a < 50 ? 50: a;
> a;
100

It may not be clear how this code works exactly because of the multiple ?. Adding parentheses makes it a little clearer:

> var a = 123;
> a = (a > 100 ? 100 : a < 50) ? 50 : a;
> a;
50
> var a = 123;
> a = a > 100 ? 100 : (a < 50 ? 50 : a);
> a;
100

?: is called a ternary operator because it takes three operands.

主站蜘蛛池模板: 石渠县| 西丰县| 桃江县| 东丽区| 通化县| 明星| 石河子市| 嘉义市| 新平| 启东市| 七台河市| 淳安县| 开鲁县| 承德县| 六盘水市| 喜德县| 赣榆县| 成都市| 来安县| 南江县| 霍州市| 通渭县| 杨浦区| 新邵县| 木兰县| 濮阳县| 新竹县| 法库县| 广水市| 盐源县| 红原县| 葵青区| 绍兴县| 胶州市| 新野县| 都匀市| SHOW| 大庆市| 嵊州市| 宾阳县| 内乡县|