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

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.

主站蜘蛛池模板: 车险| 寻乌县| 安阳县| 桓仁| 易门县| 陇南市| 钦州市| 株洲县| 鱼台县| 仪征市| 沽源县| 楚雄市| 朝阳市| 盐池县| 宣化县| 巩义市| 华坪县| 通化县| 石首市| 阿拉善右旗| 宜君县| 新巴尔虎左旗| 甘谷县| 通河县| 将乐县| 田阳县| 诏安县| 汉寿县| 儋州市| 富源县| 南投县| 绍兴市| 高要市| 凉城县| 柳林县| 德令哈市| 赤城县| 塔城市| 出国| 银川市| 西昌市|