JavaScript

JavaScript hash() function | Explained

In computer programming, the data structure is used to store, organise, and utilise the data. Javascript hash() function converts the input strings into fixed-sized data (numbers) and returns the hash value.

JavaScript hash() function is an irreparable function and governs data solidarity as it returns the same hash value for a given input. This article provides a deep insight into the JavaScript hash() function with the following learning outcomes:

– How JavaScript hash() function works

– How to use JavaScript hash() function

How JavaScripthash() function works

The hash() function transforms the large keys (strings) into small keys (numbers). It identifies the preferred digit from an array index and converts it into the hash value.

Syntax

The syntax of the hash() function is given as follows.

functionfunc(string)
{
var hash = 0;
return hash;
}

Here, func(string) is a function that takes any input and returns the hash value. The ‘0’ is the hash value of an empty string.

How to use the JavaScript hash() function

The hash() function calculates the index into an array of the slot from where it identifies the specified value. Here, we will explain the complete functionality of the hash() function with examples.

Example 1: How to create hash value using the hash() function

The hash() function creates a hash value by converting the large key into small fixed hashes.

This example shows how to create a hash value using the hash() function by converting an integer into 32 bits.

functionfunc(string) {
var hash = 0;
if (string.length == 0) return hash;
for (x = 0; x <string.length; x++) {
ch = string.charCodeAt(x);
        hash = ((hash <<5) - hash) + ch;
        hash = hash & hash;
    }
return hash;
}
var str = "Happymoments"
console.log(func(str))

A function ‘string ’ is passed with ‘0’ as a variable hash. It returns ‘0’ if the variable length is ‘0’. The string ‘Happymoments’ is stored in the function. When the hash returns, it converts the integer into 32 bits.

The output showed that the string ‘’happymoment is converted into a small hash value ‘-1245757517’ using the hash() function.

Example 2: How to convert a string into a hash value using the hash() function

The hash() function converts a string into a hash, hash number, or hash value. This example shows the conversion of a string into a fixed hash.

functionfunc(string) {
var hash = 5;
if (string.length == 5) return hash;
for (a = 5; a <string.length; a++) {
ch = string.charCodeAt(a);
        hash = ((hash <<5) - hash) + ch;
        hash = hash & hash;
    }
return hash;
}
var str = "LinuxHint"
console.log(func(str))

Here, in the above code a string is passed with a variable hash ‘5’ of total length, then it will return the hash value of the string ‘LinuxHint’.

The output showed that the string is converted to a hash value that is ‘253386’ using has() function.

Conclusion

The hash() function takes the string as an input and transforms it into the hash value as an output. This descriptive guide provides a deep insight into the hash() function in JavaScript. The working of the hash() function is provided by using the syntax. For a better understanding of hash() function, we have provided a set of examples that illustrate the usage of hash() function.

About the author

Adnan Shabbir