JavaScriptで2つの文字列が等しくないか確認する
原題: Check if two Strings are NOT equal in JavaScript | bobbyhadz
分析結果
- カテゴリ
- AI
- 重要度
- 54
- トレンドスコア
- 18
- 要約
- この記事では、JavaScriptを使用して2つの文字列が等しくないかを確認する方法について説明しています。具体的には、厳密な比較演算子(===)や非厳密な比較演算子(==)を使った例を示し、文字列の比較における注意点やベストプラクティスについても触れています。
- キーワード
Check if two Strings are NOT equal in JavaScript | bobbyhadz Check if two Strings are NOT equal in JavaScript Borislav Hadzhiev Last updated: Mar 2, 2024 Reading time · 2 min # Check if two Strings are NOT equal Use the strict inequality (!==) operator to check if two strings are not equal, e.g. a !== b . The strict inequality operator returns true if the strings are not equal and false otherwise. index.js Copied! const a = 'one' ; const b = 'two' ; if ( a !== b ) { // 👇️ this runs console . log ( '✅ strings are NOT equal' ) ; } else { console . log ( '⛔️ strings are equal' ) ; } The code for this article is available on GitHub We used the strict inequality (!==) operator to check if two strings are not equal. The operator returns a boolean result: true if the values are not equal false if the values are equal The strict inequality (!==) operator is the negation of the strict equality (===) operator. Here are some more examples of using the strict inequality (!==) operator. index.js Copied! console . log ( 5 !== '5' ) ; // 👉️ true console . log ( 'one' !== 'one' ) ; // 👉️ false console . log ( 'one' !== 'ONE' ) ; // 👉️ true console . log ( '' !== ' ' ) ; // 👉️ true console . log ( 5 !== 5 ) ; // 👉️ false console . log ( 5 !== 10 ) ; // 👉️ true console . log ( true !== true ) ; // 👉️ false console . log ( true !== false ) ; // 👉️ true console . log ( [ ] !== [ ] ) ; // 👉️ true console . log ( { } !== { } ) ; // 👉️ true console . log ( null !== null ) ; // 👉️ false console . log ( undefined !== undefined ) ; // 👉️ false console . log ( NaN !== NaN ) ; // 👉️ true The strict inequality (!==) operator considers two values of different types to be different, as opposed to the loose inequality (!=) operator. Two values of different types will never be equal when using the strict inequality operator. It's always recommended to use the strict operators (!==, ===) because they don't try to coerce the values to the same type before comparing them. The strict operators (!==, ===) produce more predictable results. Notice that in the last example, NaN !== NaN returns true . This is because NaN (not a number) is the only value in JavaScript that is not equal to itself. One common use case for the loose equality (==) and loose inequality (!=) operators is when checking if a value is nullish. index.js Copied! const a = null ; if ( a == null ) { // 👇️ this runs console . log ( 'a is null or undefined' ) ; } else { console . log ( 'a is not null and undefined' ) ; } if ( a != null ) { console . log ( 'a is not null and undefined' ) ; } The code for this article is available on GitHub The loose equality operator (==) considers null and undefined to be equal which is not the case when using the strict equality operator (===). index.js Copied! console . log ( null == undefined ) ; // 👉️ true console . log ( null === undefined ) ; // 👉️ false This is why the condition if (a != null) evaluates to true if a is not equal to null and undefined . This is the most common use case for the loose equality (==, !==) operators in JavaScript. If you use the loose equality operator to check if two values are not equal, you might get confusing results. If you want to read more about how the loose equality operators work, check out this section of the MDN docs. I wrote a book in which I share everything I know about how to become a better, more efficient programmer. You can use the search field on my Home Page to filter through all of my articles. Share Share Share Share Share Borislav Hadzhiev Web Developer GitHub Linkedin About Contacts Policy Terms & Conditions GitHub Linkedin Copyright © 2026 Borislav Hadzhiev Search for posts 0 ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... ......... .........