---
layout: page
title: "JavaScript soundex function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/soundex:520
- /functions/view/soundex
- /functions/view/520
- /functions/soundex:520
- /functions/520
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's soundex

{% codeblock strings/soundex.js lang:js https://raw.github.com/kvz/phpjs/master/functions/strings/soundex.js raw on github %}
function soundex (str) {
  // From: http://phpjs.org/functions
  // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
  // +    tweaked by: Jack
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   bugfixed by: Onno Marsman
  // +      input by: Brett Zamir (http://brett-zamir.me)
  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   original by: Arnout Kazemier (http://www.3rd-Eden.com)
  // +    revised by: Rafał Kukawski (http://blog.kukawski.pl)
  // *     example 1: soundex('Kevin');
  // *     returns 1: 'K150'
  // *     example 2: soundex('Ellery');
  // *     returns 2: 'E460'
  // *     example 3: soundex('Euler');
  // *     returns 3: 'E460'
  str = (str + '').toUpperCase();
  if (!str) {
    return '';
  }
  var sdx = [0, 0, 0, 0],
    m = {
      B: 1,
      F: 1,
      P: 1,
      V: 1,
      C: 2,
      G: 2,
      J: 2,
      K: 2,
      Q: 2,
      S: 2,
      X: 2,
      Z: 2,
      D: 3,
      T: 3,
      L: 4,
      M: 5,
      N: 5,
      R: 6
    },
    i = 0,
    j, s = 0,
    c, p;

  while ((c = str.charAt(i++)) && s < 4) {
    if (j = m[c]) {
      if (j !== p) {
        sdx[s++] = p = j;
      }
    } else {
      s += i === 1;
      p = 0;
    }
  }

  sdx[0] = str.charAt(0);
  return sdx.join('');
}
{% endcodeblock %}

 - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/strings/soundex.js)

Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are 
the closest match to this hashtable-like data structure. 

Please also note that php.js offers community built functions and goes by the 
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online 
functions that are far from perfect, in the hopes to spark better contributions. 
Do you have one? Then please just: 

 - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/strings/soundex.js)

### Example 1
This code
{% codeblock lang:js example %}
soundex('Kevin');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'K150'
{% endcodeblock %}

### Example 2
This code
{% codeblock lang:js example %}
soundex('Ellery');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'E460'
{% endcodeblock %}

### Example 3
This code
{% codeblock lang:js example %}
soundex('Euler');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
'E460'
{% endcodeblock %}


### Other PHP functions in the strings extension
{% render_partial _includes/custom/strings.html %}
