0
2

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 2019-10-06

長らく使う機会が無いと、忘れちゃっている事ってありますよね。
何度もGoogle 検索をかけてしまうjavascript の命令があります。
そんな項目を自分のためにメモしておこうかと思います。

32ビット unsigned int

32ビット integer をunsigned に変換するには

example.js
var num = 0xDF0A212;
var numb=num<<4;
console.log(numb);    //-552984288 と表示
numb=(numb&0xFFFFFFFF) >>> 0;
console.log(numb);    //3741983008 と表示

円周率

example.js
Math.PI

小文字piと書いてしまい苦労してしまいました。

文字→数字、数字→文字変換

example.js
var a=28;
console.log(a.toString(10)); // "18" と表示
console.log(a.toString(16));  // "1C" と表示
console.log(parseInt("10") + parseInt("20"));		//30
console.log(parseFloat("10.5") + parseFloat("20.3"));	//30.8
console.log(Number("105e2") + Number("203e2"));		//30800

配列の初期化

大、中、小括弧のどれだったか、すぐ忘れてしまいます。

example.js
var tmp=[];
tmp=[0,1,2];
console.log(tmp);

var tmp2=[];
tmp2=[[0,1,2],[11,12,13]];
console.log(tmp2);

文字列の文字数をカウントする

exsample.js
var name="abscdef";
console.log(name.length);

console.log で改行したくない

console.log に変えて、process.stdout.writeを使います。

exsample.js
var name="abscdef";
for(var i=0; i<name.length; i++){
    process.stdout.write(name[i]);
}
process.stdout.write('\n');

連想配列

exsample.js
var meibo=[];
meibo['Jason Voorhees']=[];
meibo['Jason Voorhees'].height=192;
meibo['Jason Voorhees'].weight=114;
meibo['Freddy Krueger']=[]
meibo['Freddy Krueger'].height=170;
meibo['Freddy Krueger'].weight=73;
meibo['Bubba Sawyer']=[]
meibo['Bubba Sawyer'].height=182.88;
meibo['Bubba Sawyer'].weight=106.6;

参照は以下のように記述します。

console.log(meibo['Jason Voorhees']);
exsample.js
var meibo2=[
    { name:'Jason Voorhees', height:192, weight:114 },
    { name:'Freddy Krueger', height:170, weight:73 },
    { name:'Bubba Sawyer', height:182.88, weight:106.6 },
];

参照は以下のように記述します。

console.log(meibo2[1]);

文字列を分割

分割するキーワードをsepstrに設定します。ここではスペースです。
要素の数はlengthでわかります。

example.js
var line={"ab cd ef gh"};
var sepstr = " ";
var p = line.split(sepstr);
console.log(p.length);

setInterval

  • 設定時間ごとに起動するsetInterval
example.js
var count = 0;
var t=setInterval(function() {
    console.log(count++);
	if(count>=10){
		clearInterval(t);
	}
}, 1000);
  • 設定時間後に起動するsetTimeout
example.js
setTimeout(function(){
    console.log("timeout\n");
}, 1000);

こちらを参照しています。
https://techacademy.jp/magazine/5537

0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?