Skip to content

Instantly share code, notes, and snippets.

@Konard
Last active December 26, 2020 11:29
Show Gist options
  • Save Konard/c00e179261a80f52421473615585e6f7 to your computer and use it in GitHub Desktop.
Save Konard/c00e179261a80f52421473615585e6f7 to your computer and use it in GitHub Desktop.
A way to make JavaScript great again. This method works as excepted with all objects that have valueOf function defined. Note: this method allows to compare Arrays without loops, you can also create your own comparable objects using valueOf function definition.
function equal(a, b) { return (a >= b) && (a <= b); }
> function equal(a, b) { return (a >= b) && (a <= b); }
< undefined
> equal({ valueOf: function () { return 10} }, { valueOf: function () { return 10} })
< true
> equal({ valueOf: function () { return 10} }, { valueOf: function () { return 20} })
< false
> equal(new Date(100), new Date(100))
< true
> equal(new Date(100), new Date(200))
< false
> equal([1, 2, 3], [1, 2, 3])
< true
> equal([1, 2, 3], [1, 2, 4])
< false
> equal(1, 1)
< true
> equal(1, 2)
< false
> equal('a', 'a')
< true
> equal('a', 'b')
< false
> equal(true, true)
< true
> equal(true, false)
< false
> equal(null, null)
< true
> equal(null, 0)
< false
> { valueOf: function () { return 10} } == { valueOf: function () { return 10} }
< false
> { valueOf: function () { return 10} } == { valueOf: function () { return 20} }
< false
> equal(new Date(100), new Date(100))
< false
> equal(new Date(100), new Date(200))
< false
> [1, 2, 3] == [1, 2, 3]
< false
> [1, 2, 3] == [1, 2, 4]
< false
> 1 == 1
< true
> 1 == 2
< false
> 'a' == 'a'
< true
> 'a' == 'b'
< false
> true == true
< true
> true == false
< false
> null == null
< true
> null == 0
< false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment