33
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascript小技集

Last updated at Posted at 2015-05-08

実務で使う機会なんてあるか分からないけど、紹介します。

【変数同士の値交換時に新しい変数を使わない】

var x = 50,y = 100;
x = [y,y = x][0];

【配列をループさせるスムーズな書き方】

for(var i = 0,item;item = data[i++];) //配列が空か0だと終了

【最大値を超えない数値の増加】

var str = 'Hello';
var num = 0,str_len = str.length;
num++;
num %= str_len; //5以上なら0になる

【奇数の判断高速化】

var i = 1;
if(i % 2) /* ⇒ */ if(i & 1) //2進数で判断させる

【Null, Undefined, Emptyのチェック】

var i;
if(i || "") //上記と0以外ならtrue

【0と1の入れ替え、またboolの入れ替え】

var num = 0;
num ^= 1; //0⇒1
num ^= 1; //1⇒0
var bool = false;
bool = !bool; //true⇔false

三項演算子とかは皆さんご存じかと。色々あっても他人が知らないと使っても意味ないですね。

33
32
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?