Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You cannot explicitly return two variables from a single function, but there are various ways you could concatenate the two variables in order to return them.</p> <p>If you don't need to keep the variables separated, you could just concatenate them directly like this:</p> <pre><code>function test(){ var h = 'Hello'; var w = 'World'; var hw = h+w return (hw); } var test = test(); alert(test); </code></pre> <p>This would alert "HelloWorld". (If you wanted a space in there, you should use <code>var hw = h+" "+w</code> instead.</p> <p>If you need to keep the two variables separated, you can place them into an array like so:</p> <pre><code>function test(){ var h = "Hello"; var w = "World"; var hw=[h,w]; return hw; } var test = test(); alert(test); </code></pre> <p>This allows the <code>h</code> and <code>w</code> values to still be accessed individually as <code>test[0]</code> and <code>test[1]</code>, respectively. However, alert(test) here will display "Hello,World" because of the way alert() handles arrays (that is, it prints a comma-separated list of each element in the array sequentially). If you wanted to produce the same output as your example code, you would need use something like <code>join()</code>. <code>join()</code> will construct a string from an array, it takes one argument which serves as a separator between the elements. To reproduce the two alerts from my first example, you would need to use <code>alert(test.join(""))</code> and <code>alert(test.join(" ")</code>, respectively. </p> <p>My example could be shortened slightly by skipping the creation of the <code>hw</code> variable and just returning an array directly. In that case, test() would look like this:</p> <pre><code>function test(){ var h="Hello"; var w="World"; return [h, w]; } </code></pre> <p>This could also be done as an object with <code>return { h : h, w : w };</code>, in which case you would access the individual variables as test.h and test.w, respectively. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload