Spells and Charms

プログラミング(呪文学)の学習記録。

eloquent javascript 2nd edition chapter 1 Values, Types, and Operators

Values

Numbers

Arthmetic
+ //addition

- //subtraction

* //muitiplication

/ //devision

% //reminder of dividing

 

special numbers
infinity

-infinity

 NaN //not a number

 

String

Strings cannot be divided, multiplied, or subtracted, but the + operator can be used on them.

 

Unary Operator

It takes only one value.

typeof

produces a string value naming the type of the value you give it.

 

 FYI,  Binary Operator

 It takes two values. - can be both, unary and binary opertor, minus and negative.

 

Boolean Values

true
false

produce true or false

comparison
<

>

<=

>=

===

!==

 

Other similar operators are >= (greater than or equal to), <= (less than or equal to), == (equal to), and != (not equal to).

Strings can be compared.

The way strings are ordered is more or less alphabetic: uppercase letters are always “less” than lowercase ones, so "Z" < "a" is true. 

 

 

logical operators
&& //and

|| //or 

! //not

 

Conditional Operator

true ? a : b

ternary, it takes three values.

pick one of two values based on a third value.

console.log(true ? 1 : 2);
// → 1
console.log(false ? 1 : 2);
// → 2

 

Undefined Value

undefined
null

that are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.

The difference in meaning between undefined and null is an accident of JavaScript’s design, and it doesn’t matter most of the time.  

 

 

この章はエクササイズなし。

この動画も並行してみると理解が早い。
ただし英語が聞き取りにくいのと、
かなり遅いので、2倍速再生字幕付きでみました。 

 

 

ちょっと考えた事。

 logical operator を眺めていて、ふと思った事。
or のオペレーター、

a||b

って、

if(a){

}else if(b){

}

と同じなのかしら?と思ったのだが結論は、
似ているけどちがう

var x = a || b // aがundefinedとかnullならxにbを入れる
var x = if (a) a else b // こうは書けない

 

変数代入の右側は絶対にじゃないとダメ

var x = a + b
var x = a - 10
var x = 1 + 10
var x = 2 * 2 * 3.14

var x = a || b // <-これは "aがundefinedとかnullとか''とか0じゃなければa、それ以外ならb"という

var x = if (a) a else b // これは右辺が文なのでダメ。if else とかswitchとかforは

 

 

意味合いは似ているけれど、文法的には決定的に異なる。
だから、同じものではない。

 

 

ということでした。