JavaScript Tutorial 2: Variables and Operators

Javascript tutorial (学习笔记) 系列基本上转载自 “A re-introduction to JavaScript (JS Tutorial)” 加上自己学习过程中的心得体会以及自己的例子和理解. 这一篇是第二篇(接Javascript Tutorial (学习笔记) 1):

Other types

JavaScript distinguishes between null, which is an object of type ‘object’ that indicates a deliberate non-value, and undefined, which is an object of type ‘undefined’ that indicates an uninitialized value — that is, a value hasn’t even been assigned yet. We’ll talk about variables later, but in JavaScript it is possible to declare a variable without assigning a value to it. If you do this, the variable’s type is undefined.

JavaScript has a boolean type, with possible values true and false (both of which are keywords). Any value can be converted to a boolean according to the following rules:

  1. false, ``, the empty string (""), NaNnull, and undefined all become false
  2. all other values become true

You can perform this conversion explicitly using the Boolean() function:

> Boolean("")
> Boolean(234)

However, this is rarely necessary, as JavaScript will silently perform this conversion when it expects a boolean, such as in an if statement (see below). For this reason, we sometimes speak simply of “true values” and “false values,” meaning values that become true and false, respectively, when converted to booleans. Alternatively, such values can be called “truthy” and “falsy”, respectively.

Boolean operations such as && (logical and), || (logical or), and ! (logical not) are supported; see below.


New variables in JavaScript are declared using the <a title="en/Core_JavaScript_1.5_Reference/Statements/var" href="" target="_blank">var</a> keyword:

var a;
var name = "simon";

If you declare a variable without assigning any value to it, its type is undefined.

An important difference from other languages like Java is that in JavaScript, blocks do not have scope; only functions have scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function.

下面这段测试代码就是测试上面这段话讲的, javascript没有scope, 意思就是即使是在局部定义的变量(比如for里面), 该变量还是能够在全局访问到, 测试如下:

#! /usr/bin/env node
for (var i = 0; i < 5; i++) {
//Will execute 5 times
var local_var = i * 17;

console.log("i = " + i);
console.log("local_var = " + local_var);

i = 5
local_var = 68


JavaScript’s numeric operators are +-*/ and % - which is the remainder operator. Values are assigned using =, and there are also compound assignment statements such as += and -=. These extend out to x = x <em>operator</em> y.

x += 5
x = x + 5

You can use ++ and -- to increment and decrement respectively. These can be used as prefix or postfix operators.

The + operator also does string concatenation:

> "hello" + " world"
hello world

If you add a string to a number (or other value) everything is converted in to a string first. This might catch you out:

> "3" + 4 + 5
> 3 + 4 + "5"

Adding an empty string to something is a useful way of converting it.

Comparisons in JavaScript can be made using <><= and >=. These work for both strings and numbers. Equality is a little less straightforward. The double-equals operator performs type coercion (强制类型转换) if you give it different types, with sometimes interesting results:

> "dog" == "dog"
> 1 == true

To avoid type coercion, use the triple-equals operator:

> 1 === true
> true === true

There are also != and !== operators.

JavaScript also has bitwise operations. If you want to use them, they’re there.

关于比较我自己再插几句话:关于 (===) 和 (==)的区别, 三个等号的(===)在比较的时候不进行类型转换, ===两边要相等当且仅当两边的类型相同value相等. 而两个等号(==)的比较 则会进行强制类型转换, 也就是上面看到的那个例子.

Written on August 27, 2013