JavaScript: test vs exec
<p>I would like to know what's the best way to check a string for example (mail, password ..Etc).</p>

<pre><code>/^...$/i.exec(a)
</code></pre>

<p>vs</p>

<pre><code>/^...$/i.test(a)
</code></pre>

<ul>
<li><strong>exec</strong> returns value</li>
<li><strong>test</strong> true</li>
</ul>

<p><strong>test:</strong></p>

<pre><code>// 1° way
var mail = req.body.mail;
if(check(mail)){
</code></pre>

<p><strong>exec:</strong></p>

<pre><code>// 1° way
var mail = req.body.mail;
if(check(mail)){

// 2° way
var mail = check(req.body.mail);
if(mail){
</code></pre>

<p><strong>exec</strong> or <strong>test</strong> ? and what number ( 1° or 2° if exec)</p>

<p><strong>SOLUTION</strong></p>

<p><strong>test</strong> is better for this case.</p>

<ul>
<li>it will surely be faster.</li>
</ul>

<p>But the most important</p>

<ul>
<li><strong>test</strong> performs all his work. While <strong>exec</strong> did not perform, because more can be done, but we do not need.</li>
<li>Like said <em>Mattias Buelens</em>, using isMail() it's more logical: is an email: yes or no. While <strong>exec</strong>: is an email: email or null -> wtf ? lol</li>
</ul>
