Javascript passing variable on as null?
<p>I honestly can't think of a better title, I'm new with writing my own javascript.</p>

<p>Basically, I have the following script which will translate a <code>hex</code> value to an <code>rgb</code> value:</p>

<pre><code>$(".submit").click(function() {
    function hex2rgb(hex) {
        if (hex.length &lt; 3) {
            $(".result").text("Error");
        }
        if (hex[0] == "#") {
            hex = hex.substr(1);
        }
        if (hex.length == 3) {
            var temp = hex;
            hex = '';
            temp = /^([a-f0-9])([a-f0-9])([a-f0-9])$/i.exec(temp).slice(1);
            for (var i = 0; i &lt; 3; i++) hex += temp[i] + temp[i];
        }
        var triplets = /^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(hex).slice(1);
        return {
            red: parseInt(triplets[0], 16),
            green: parseInt(triplets[1], 16),
            blue: parseInt(triplets[2], 16)
        }
    }
    var hex = $(".hex").val();
    var rgb = hex2rgb("#" + hex);
    $(".result").text("rgba(" + rgb.red + "," + rgb.green + "," + rgb.blue + ",1)");
});​
</code></pre>

<p><sub>Working example: <a href="http://jsfiddle.net/charlescarver/JkeKV/5/" rel="nofollow">http://jsfiddle.net/charlescarver/JkeKV/5/</a> </sub></p>

<p>When the number of digits entered in the textbox is less than 3 (an incomplete <code>hex</code> code), it changes the text of <code>.result</code> to error. The code itself works fine, but the console is throwing up an error saying... </p>

<blockquote>
  <p>TypeError: 'null' is not an object (evaluating
  '/^([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i.exec(hex).slice')</p>
</blockquote>

<p>I assumed that if the code was less than 3 digits, it would just display the error, not try to move on to <code>if (hex.length == 3) {}</code> function, which it seems like it is doing. Is any of this correct?</p>

<p>Also, would it be better to use <code>if (hex.length == 3) {}</code> or <code>if (hex.length &gt;= 3) {</code> for the function that translates the <code>hex</code> to <code>rgb</code>? I'm only thinking this because the number of digits has to be <code>3 &gt;= x &lt;= 6</code>?</p>
