Updated April 10, 2023
Definition of JavaScript hash()
Hash function in Javascript is any function that takes input as arbitrary size data and produces output as fixed-size data. Normally, the returned value of the hash function is called hash code, hash, or hash value. As already mentioned, hash returns the fixed size, which means that whatever size the input data is, a fixed size data will be received after processing the input data with a hash function. Let us look into more details on hash function in the following sections, such as syntax, working, examples, and advantages.
Syntax:
Below is the syntax of the hash function.
function func(string)
{
//set variable hash as 0
var hash = 0;
.. . .. . .
return hash;
}
Here, the hash value will be returned if this function is called.
How does hash() Function Works in JavaScript?
In a Hash Table, a hash function will be used for computing the index into an array of slots or buckets, from which the preferred value can be identified. Suppose the following code has to be used for building.
constemp = {
name: 'Anna',
place: 'Canada',
age: 29,
}
The array of the same will be as shown below.
constemp = [29, 'Anna', 'Canada']
In order to get the right index of name, place and age, hash functions have to be called as shown below.
- For getting the index of name, hashfunc(“name”) will be called, and1 will be returned.
- For getting the index of place, hashfunc(“place”) will be called, and 2 will be returned.
- For getting the index of age, hashfunc(“age”) will be called, and 0 will be returned.
As there is no particular order in the array, the index of the array is merely bound to the key. Now, let us see the hash function constraints:
1. It Should be Deterministic
Suppose we input the same key; the same array index should be returned. Otherwise, the value won’t be found as the data doesn’t alter its position in the array.For example, let us consider const hash1 = (num) =>num % 9 which has the deterministic property. At the same time, const hash2 = (num) =>num % 9 + Math.random() won’t be used, as for different calls of hash2 will obtain different results for the same input. Moreover, these hash functions sometimes get an external parameter for hashing. Suppose, const hash3 = (num, param) =>num % param, hash3 will have deterministic property, as for the same data given as input and the similar parameters, the same output will be returned.
2. It Should Be Fast
The hash function has to be used every time we create, read, update, or delete data. Therefore, the hash function should be fast, and moreover, it should not be connected to the existing data length. That is, O(1).
3. It Should Be Uniform Distributed
Suppose there is an array of length 3. If 3 more places have to be added to an array with 2 positions to store data, 2 values have to be stored in 1 place. As this situation causes collision(two values place at one position), which leads to more computational work, uniform distribution has to be done for array indexes. For example, in the previous example, hash1(1) = hash1(10). It is important that for a good hash function, two different values for a and b should be considered (hash(a) = hash(b)). Algorithms such as brute force are available to identify such values for good hash functions. But, for several hash functions, on a normal group of supercomputers, millions of years has to be needed to find such values.
4. It Should Be Non-Invertible
The final important property of the hash function is the non-invertible property. It means, if a hash code is available, original data cannot be recovered without using a lot of resources for computing. Moreover, it cannot find original data. That is, if a hash code h is available, value x cannot be found, that hash(x) = h. Now, let us see how a hash function has to be created.
Create a function func and set the hash variable as 0. Then perform the coding as needed. Below is the sample hash function.
function func(string) {
//set variable hash as 0
var hash = 0;
// if the length of the string is 0, return 0
if (string.length == 0) return hash;
for (i = 0 ;i<string.length ; i++)
{
ch = string.charCodeAt(i);
hash = ((hash << 5) - hash) + ch;
hash = hash & hash;
}
return hash;
Example of JavaScript hash()
Let us see the sample program on the hash function in javascript.
Example #1
Javascript program to create a hash function that returns a hash code.
Code:
<!DOCTYPE html>
<html>
<head>
<title>
Create hash from string
</title>
<style>
h1 {
color: red;
}
</style>
<style>
h3 {
color: green;
}
</style>
</head>
<body>
<center>
<h1>Sample output</h1>
<h3>Creation of hash from string </h3>
<p>Hash value of the given string 'Happy moments' is :</p>
<p id="moment"></p>
<script>
// Conversion of an integer to 32bits
function func(string) {
//set variable hash as 0
var hash = 0;
// if the length of the string is 0, return 0
if (string.length == 0) return hash;
for (i = 0 ;i<string.length ; i++)
{
ch = string.charCodeAt(i);
hash = ((hash << 5) - hash) + ch;
hash = hash & hash;
}
return hash;
}
// string that has to create hashcode
varstr = "Happymoments"
document.getElementById("moment").innerHTML
= func(str);
</script>
</center>
</body>
</html>
Output:
First, create titles and headers. Once it is created, create a hash function that converts an integer to 32bits. In that function, the first set variable hash as 0. If the length of the string is 0, return 0. Otherwise, perform the code in for loop, which gives the input string as Happy moments. On executing the code, texts in headers that have colors and a hash value will be returned.
Recommend ed Articles
This is a guide to JavaScript hash(). Here we also discuss the introduction and how the hash() function works in javascript? along with an example. You may also have a look at the following articles to learn more –