Skip to content

Instantly share code, notes, and snippets.

@adamloving
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/65d4f2cc3dda1e1ce910 to your computer and use it in GitHub Desktop.
Save adamloving/65d4f2cc3dda1e1ce910 to your computer and use it in GitHub Desktop.
javascript string compare.
if (1 == '1') console.log('1. 1 == "1"');
if (1 === '1') {} else console.log('2. 1 is not === "1" (types different)');
var a = 'a';
var a1 = a;
var a2 = 'a';
if (a == a1) console.log('3. a == a1');
if (a == a2) console.log('4. a == a2');
if (a === a2) console.log('5. even a ==== a2 (equal value and type)');
if ('0' < 'A') console.log('6. "0" is less than "A"');
if ('A' < 'a') console.log('7. "A" is less than "a"');
if ('a' < 'aa') console.log('8. "a" is less than "aa"');
var strings = ['a', 'b', 'A', '!', 'z', 'Z', 'Alpha', 'alpha', '0', '1', '111'];
console.log('9. sorted strings', strings.sort());
@adamloving
Copy link
Author

Output is

1. 1 == "1"
2. 1 is not === "1" (types different)
3. a == a1
4. a == a2
5. even a ==== a2 (equal value and type)
6. "0" is less than "A"
7. "A" is less than "a"
8. "a" is less than "aa"
9. sorted strings [ '!', '0', '1', '111', 'A', 'Alpha', 'Z', 'a', 'alpha', 'b', 'z' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment