Help us understand the problem. What is going on with this article?

javascript小技集

More than 3 years have passed since last update.

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

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

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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away