If you want a hashCode() function like Java's in JavaScript, that is yours:

Copyfunction hashCode(string){
    var hash = 0;
    for (var i = 0; i < string.length; i++) {
        var code = string.charCodeAt(i);
        hash = ((hash<<5)-hash)+code;
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}

That is the way of implementation in Java (bitwise operator).

Please note that hashCode could be positive and negative, and that's normal, see HashCode giving negative values. So, you could consider to use Math.abs() along with this function.

Answer from KimKha on Stack Overflow
🌐
GitHub
gist.github.com › hyamamoto › fd435505d29ebfa3d9716fd2be8d42f0
JavaScript Implementation of String.hashCode() . · GitHub
/** * @see http://stackoverflow.com/q/7616461/940217 * @return {number} */ String.prototype.hashCode = function(){ if (Array.prototype.reduce){ return this.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); } var hash = 0; if (this.length === 0) return hash; for (var i = 0; i < this.length; i++) { var character = this.charCodeAt(i); hash = ((hash<<5)-hash)+character; hash = hash & hash; // Convert to 32bit integer } return hash; }
Discussions

How does one deterministically hash an object in javascript? I tried to look for the answer in their github repo, but I couldn't figure it out.
/* eslint-disable no-bitwise */ // A simple, *insecure* 32-bit hash that's short, fast, and has no dependencies. // Output is always 7 characters. // Loosely based on the Java version; see // https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript const simpleHash = (str: string): string => { let hash = 0; for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = (hash << 5) - hash + char; } // Convert to 32bit unsigned integer in base 36 and pad with "0" to ensure length is 7. return (hash >>> 0).toString(36).padStart(7, '0'); }; export default simpleHash;/* eslint-disable no-bitwise */ // A simple, *insecure* 32-bit hash that's short, fast, and has no dependencies. // Output is always 7 characters. // Loosely based on the Java version; see // https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript const simpleHash = (str: string): string => { let hash = 0; for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = (hash << 5) - hash + char; } // Convert to 32bit unsigned integer in base 36 and pad with "0" to ensure length is 7. return (hash >>> 0).toString(36).padStart(7, '0'); }; simpleHash(JSON.stringify(myObject)); More on reddit.com
🌐 r/sveltejs
5
3
March 15, 2022
Converting objects to hash signatures in JavaScript
Better collision properties, easier to implement, and a cryptographic hash to boot, just as long as you don’t have circular references and only care about the serializable “contents” of an object · In addition, the containers need to have these same comparators, so all your data structures ... More on news.ycombinator.com
🌐 news.ycombinator.com
17
23
February 14, 2019
String.hashCode() is plenty unique

I think folks sometimes forget that hashcodes aren't intended to be 100% unique, just a first order approximation that's distributed well enough for hashtable buckets. True equality is why equals() exists.

It isn't as if a hashcode collision will cause a set to deduplicate your object or overwrite your value in a map. That's what equals is for.

More on reddit.com
🌐 r/programming
222
422
October 13, 2016
Creating a Hash from a String in JavaScript
I want to transform strings into hash values using JavaScript. Is there a way to accomplish this solely with client-side code, since I’m not using any server-side languages? You can explore options like the cryptographic hash function in JavaScript libraries. More on community.latenode.com
🌐 community.latenode.com
0
October 9, 2024
🌐
The Daily Signal
lowrey.me › implementing-javas-string-hashcode-in-javascript
Implementing Java's String.hashCode in JavaScript
April 23, 2018 - In Java, each string has the method hashCode() on the object. It returns a 32 bit integer that is relatively guaranteed to be unique for any given string. JavaScript has no similar comparable utility.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-create-hash-from-string-in-javascript
How to Create Hash From String in JavaScript? - GeeksforGeeks
July 12, 2025 - To create a hash from a string in JavaScript, you can use hashing algorithms like MD5, SHA-1, or SHA-256.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Hash_function
Hash function - Glossary | MDN
A hash function is a function that takes a variable-length input and produces a fixed-length output, also called a digest (or just a "hash"). Hash functions should be quick to compute, and different inputs should as far as possible produce different outputs (this is called collision-resistance).
🌐
Pmav
pmav.eu › stuff › javascript-hash-code
Javascript HashCode Function
var object1 = []; var object2 = { f1 : function() { var i; }, a : [1, 2, "#", { m : function() { return 1; } }]}; var object3 = object2; var object4 = function(arg) { arg++ }; var object5 = function(arg) { arg++ }(2); var object6 = (function(arg) { arg++ })(2); HashCode.value(object1); // result: d41d8cd98f00b204e9800998ecf8427e HashCode.value(object2); // result: e72b9ba8d4c160ab0d1b2a828359dde1 HashCode.value(object3); // result: e72b9ba8d4c160ab0d1b2a828359dde1 HashCode.value(object4); // result: 16619c0e50afd2695fc1a22dab652395 HashCode.value(object5); // result: be36fcbc3ab6342a7cbcb2e37d2ae78f HashCode.value(object6); // result: be36fcbc3ab6342a7cbcb2e37d2ae78f · All the source code and examples are online here. Javascript HashCode Function | pmav.eu | 23/Jan/2010 | Valid HTML 4.01 Strict | This work is licensed under a MIT License.
Find elsewhere
🌐
Immutable-js
immutable-js.com › docs › v5 › hash()
hash() — Immutable.js
When designing Objects which may be equal, it's important that when a .equals() method returns true, that both values .hashCode() method return the same value.
🌐
npm
npmjs.com › package › js-hash-code
js-hash-code - npm
August 25, 2017 - algo (String|Function): The hash algorithms. default like JAVA hashCode. set (Boolean): ignore collection is object or array, has same elements, hash code is same, if true. Returns (string): Returns the javascript object hash code.
      » npm install js-hash-code
    
Published   Aug 25, 2017
Version   1.0.0
Author   Robin JH Kang
🌐
Reddit
reddit.com › r/sveltejs › how does one deterministically hash an object in javascript? i tried to look for the answer in their github repo, but i couldn't figure it out.
r/sveltejs on Reddit: How does one deterministically hash an object in javascript? I tried to look for the answer in their github repo, but I couldn't figure it out.
March 15, 2022 -

See this excerpt from vue-query:

So the key is based on an array, which can contain primitives and objects. And while the order of the array does matter, apparently the order of the object's keys do not matter.

I tried to read through their code on GitHub to figure out how they hash that key and how it guarantees that it ignores the order of keys in objects. But I couldn't figure it out. Any help here would be greatly appreciated. 🙏

🌐
Myridia
myridia.com › dev_posts › view › 4334
create a hashcode from javascript
July 17, 2025 - https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript · Fold knowledge into data so program logic can be stupid and robust
🌐
IBM
ibm.com › docs › SSVRGU_9.0.0 › com.ibm.designer.domino.api.doc › r_wpdr_runtime_locale_hashcode_r.html
hashCode (JavaScript)
Override hashCode. Since Locales are often used in hashtables, caches the value for speed.
🌐
Hacker News
news.ycombinator.com › item
Converting objects to hash signatures in JavaScript | Hacker News
February 14, 2019 - Better collision properties, easier to implement, and a cryptographic hash to boot, just as long as you don’t have circular references and only care about the serializable “contents” of an object · In addition, the containers need to have these same comparators, so all your data structures ...
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › math › hash string into number
Implement the SDBM hash function in JavaScript - 30 seconds of code
March 3, 2024 - In order to implement it in JavaScript, you can use String.prototype.split() and Array.prototype.reduce() to create a hash of the input string, utilizing bit shifting as described above. const sdbm = str => { let arr = str.split(''); return arr.reduce( (hashCode, currentVal) => (hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode), 0 ); }; sdbm('name'); // -3521204949 ·
🌐
Reddit
reddit.com › r/programming › string.hashcode() is plenty unique
r/programming on Reddit: String.hashCode() is plenty unique
October 13, 2016 - Even with a perfectly designed hash code function you will start to see collisions at around 216 entries. hashCode returns an int (32 bits) and by the birthday paradox you have ~50% chance of having at least one collision with 216 entries.
🌐
npm
npmjs.com › search
hashCode - npm search
A JavaScript implementation of Java's hashCode method.
🌐
Quora
quora.com › Do-Map-and-Set-in-JavaScript-store-the-hashcode-of-an-object-as-the-key-or-is-there-some-other-methodology-to-make-these-data-structures-work-Since-objects-in-JS-can-only-have-strings-as-keys
Do Map and Set in JavaScript store the hashcode of an object as the key or is there some other methodology to make these data structures work? (Since objects in JS can only have strings as keys). - Quora
Answer: User-9594798534900656094 provides a good technical breakdown. I wanted to offer a slightly more simplistic way to think about this. First, Sets are a new type of native object in JavaScript. I think of them like a unique array I don’t have to deduplicate, with some level of type sensitiv...
🌐
Latenode
community.latenode.com › other questions › javascript
Creating a Hash from a String in JavaScript - JavaScript - Latenode Official Community
October 9, 2024 - I want to transform strings into hash values using JavaScript. Is there a way to accomplish this solely with client-side code, since I’m not using any server-side languages? You can explore options like the cryptographic hash function in JavaScript libraries.