Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
1640

Go to list of users who liked

1190

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 3 years have passed since last update.

console.log(); しか使えなかった自分へ。。。

Last updated atPosted at 2022-05-19

この記事について

Webエンジニアになって早1年半。railsのデバッグをする時にはエディターのデバッガーでスマートにできていたが、javascriptになるといつもconsole.log();ばかりを使って原始的なデバッグをしていた。。。

そんな脳筋な過去の自分に教えてやるための記事です。

console.log({変数名});

「いきなりconsole.log();の紹介かい!!!」って思われるかもしれませんが、この技を知ったときは「なんで知らんかったんや。。。」って思うくらい便利だったので最初に紹介します。

以下のようなHTMLがある場合

<form><inputtype="text"value="名無しの権兵衛"id="name"><inputtype="text"value="80歳"id="age"><inputtype="text"value="バスケ"id="sport"></form>

それぞれのinputの要素・値を取得します。

letnameForm=document.getElementById('name');letageForm=document.getElementById('age');letsportForm=document.getElementById('sport');letname=nameForm.value;letage=ageForm.value;letsport=sportForm.value;

上記で取得したinput要素の値を確認しようと思ったら、どれがどのinput要素の値なのかが分かるようにconsole.log(変数名: ${変数});と、変数展開して書いていたが、書くのが非常に面倒やった。。。

console.log(`name:${name}`);console.log(`age:${age}`);console.log(`sport:${sport}`);

わざわざコロン区切りに書かなくてもconsole.log({変数名});で同じ出力結果になる!!!!

console.log({name});console.log({age});console.log({sport});

スクリーンショット 2022-05-19 16.54.32.png

追記

@fukken さんにご教示頂きました。
知らないことを学べるので、このような補足コメントは嬉しいです。ありがとうございました。

{name: name} を {name} と書けるのはconsoleの機能じゃなくてES2015の記法(つまりconsoleに限らずモダンブラウザならJS内のどこでも使える)なので、念の為。

console.time();

処理にかかっている時間が知りたいなーという時は、該当の処理の場所をconsole.time();console.timeEnd();で挟んでやる!!!
そうすることで、タイマー開始からの経過時間がミリ秒単位で出力される。

console.time();letinput=document.getElementsByClassName('input');console.log(input)console.timeEnd();

スクリーンショット 2022-05-19 17.54.09.png

console.assert();

第1引数に渡された条件式がfalseになった場合、エラー形式で第2引数の内容をログとして出力させることができて、trueの場合は何も出力されない。

constisEven=num=>num%2===0;console.assert(isEven(5),'奇数やで〜');console.assert(isEven(10),'これは出力されへんで〜');

スクリーンショット 2022-05-20 9.03.31.png

console.table();

配列・オブジェクトの内容を表形式で表現したものを出力してくれる!!!

大量のinput要素があって、1つずつ要素を確認したいとなった時。。。

<form><inputtype="text"value="1"class="input"><inputtype="text"value="2"class="input"><inputtype="text"value="3"class="input"><inputtype="text"value="4"class="input"><inputtype="text"value="5"class="input"><inputtype="text"value="6"class="input"><inputtype="text"value="7"class="input"><inputtype="text"value="8"class="input"><inputtype="text"value="9"class="input"><inputtype="text"value="10"class="input"><inputtype="text"value="11"class="input"><inputtype="text"value="12"class="input"><inputtype="text"value="13"class="input"><inputtype="text"value="14"class="input"><inputtype="text"value="15"class="input"><inputtype="text"value="16"class="input"><inputtype="text"value="17"class="input"><inputtype="text"value="18"class="input"><inputtype="text"value="19"class="input"><inputtype="text"value="20"class="input"></form>

単純に以下のように取得しても、なんの事かよぉー分からんことになっている

letinput=document.getElementsByClassName('input');console.log(input);

スクリーンショット 2022-05-19 17.26.26.png

これを見やすく表形式にできる技がconsole.table();なのだ!!!!

letinput=document.getElementsByClassName('input');console.table(input);

スクリーンショット 2022-05-19 17.28.04.png

「必要な情報だけが見たい!!!」という時は、第2引数に配列で指定したらいいだけ。

letinput=document.getElementsByClassName('input');console.table(input,["value"]);

スクリーンショット 2022-05-19 17.30.07.png

最後に

chromeも上手いこと使ってデバッグしてね!!!!!

1640

Go to list of users who liked

1190
11

Go to list of comments

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
1640

Go to list of users who liked

1190

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp