Skip to content

Instantly share code, notes, and snippets.

JavaScriptではよく「==」ではなく「===」を使ったほうがいいと聞きますが、いまいち理由がわかっていなかった(なんか厳密になる?くらい)のでまとめました。

== は引数の型が異なる時に使ってはいけない

よく聞く「==は厳密な比較ができない」というのは、__引数の型__に問題があります。

JavaScriptでは暗黙の型変換がありますので、== による比較によって比較対象の型が意図しない方法で比較される恐れがあります。

Effective JavaScriptによると、==演算子によって強制的に引き起こされる振る舞いは以下のようになります。

@y-temp4
y-temp4 / settings.json
Created August 15, 2019 13:48
vetur defaultFormatter settings
{
"vetur.format.defaultFormatter.html": "none",
"vetur.format.defaultFormatter.js": "prettier-eslint",
"vetur.format.defaultFormatter.ts": "prettier"
}
@y-temp4
y-temp4 / tailwind.config.js
Created August 9, 2019 02:23
Use open-color for Tailwind CSS
const openColors = require('open-color/open-color.json')
const colors = {}
Object.entries(openColors).forEach(([color, values]) => {
if (['white', 'black'].includes(color)) return
colors[color] = { ...values }
})
module.exports = {
@y-temp4
y-temp4 / observe-window-onbeforeunload.js
Last active July 9, 2019 04:22
window.onbeforeunload の変更を検知して「このサイトを離れますか?」のポップアップを表示しないようにする
Object.defineProperty(window, 'onbeforeunload', {
set(newValue) {
if (typeof newValue === 'function') window.onbeforeunload = null;
}
});
@y-temp4
y-temp4 / file1.js
Last active December 24, 2018 05:27
Nuxt.js を用いた新規事業開発を半年以上経験して得た知見 ref: https://qiita.com/y-temp4/items/b8cbedbd95163da82911
async getLikesCount({ commit }, { articleId }) {
const { count: likesCount } = await this.$axios.$get(`/articles/${articleId}/likes`)
return likesCount
}
@y-temp4
y-temp4 / file0.txt
Last active November 14, 2018 04:55
brewでインストールしたyarnのバージョンを変更する方法 ref: https://qiita.com/y-temp4/items/ed73daa00feeda494ad9
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/fba7635ab69384ac980c635483a7af825bc06088/Formula/yarn.rb
function aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() {
return true
}
@y-temp4
y-temp4 / test.js
Created September 14, 2018 07:00
Test Gist Code.
function test(arg) {
console.log(arg)
return arg
}
@y-temp4
y-temp4 / .babelrc
Created December 20, 2017 23:25
【初心者向け】NEM LibraryとJavaScriptを使った開発にちょっとだけ入門してみる ref: https://qiita.com/y-temp4/items/9be4b041565a4bb24dab
{
"presets": ["env"]
}
@y-temp4
y-temp4 / file0.txt
Created November 21, 2017 07:16
Jestが動かないときの対処法 ref: https://qiita.com/y-temp4/items/d08483883c24bc31f5b6
$ brew uninstall watchman