Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1134519 of return

  • 리비전 슬러그: Web/JavaScript/Reference/Statements/return
  • 리비전 제목: return
  • 리비전 아이디: 1134519
  • 제작일시:
  • 만든이: Diana_
  • 현재 리비전인가요?
  • 댓글 'return statement' was not translated by Korean.

리비전 내용

{{jsSidebar("Statements")}}

return statement는 함수 실행을 종료하고, function을 호출한 caller에게 되돌려줄 값을 지정합니다.

Syntax

return [[expression]]; 
expression
만약 return 표현식에서 return을 생략할 경우에는 undefined가 반환 됩니다.

Description

함수에서 return statement가 호출될 경우, 함수는 더 이상 실행되지 않습니다. 만약 return 값이 주어진다면, 그 값이 function을 호출한 caller에 반환됩니다. return이 생략되는 경우에는 undefined가 반환됩니다. 아래의 return statement들은 모두 함수의 실행을 중단시킵니다. 

return;
return true;
return false;
return x;
return x + y / 3;

Automatic Semicolon Insertion

return statement는 automatic semicolon insertion (ASI)의 영향을 받습니다. 줄바꿈으로는 return 키워드와 그 뒤에 나오는 표현식을 분리할 수 없습니다.

return
a + b;

이것을 ASI로 변형시키면 다음과 같습니다 :

return; 
a + b;

이것을 실행시키면 console은 "return statement 이후에 도달할 수 없는 코드가 있습니다"라는 경고를 띄웁니다.

Starting with Gecko 40 {{geckoRelease(40)}}, a warning is shown in the console if unreachable code is found after a return statement.

Examples

return

아래의 함수는 x가 숫자일 때, x의 제곱을 반환합니다.

function square(x) {
   return x * x;
}

Interrupt a function

함수는 return이 쓰여지는 지점에서 즉시 실행을 멈춥니다.

function counter() {
  for (var count = 1; ; count++) {  // infinite loop
    console.log(count + "A"); // until 5
      if (count === 5) {          
        return;
      }
      console.log(count + "B");  // until 4
    }
  console.log(count + "C");  // never appears
}

counter();

// Output:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A

Returning a function

Closures에 대해서도 더 알아보세요.

function magic(x) {
  return function calc(x) { return x * 42 };
}

var answer = magic();
answer(1337); // 56154

Specifications

Specification Status Comment
{{SpecName('ES1')}} {{Spec2('ES1')}} Initial definition.
{{SpecName('ES5.1', '#sec-12.9', 'Return statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-return-statement', 'Return statement')}} {{Spec2('ES6')}}  
{{SpecName('ESDraft', '#sec-return-statement', 'Return statement')}} {{Spec2('ESDraft')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

리비전 소스

<div>{{jsSidebar("Statements")}}</div>

<p><strong>return statement</strong>는 함수 실행을 종료하고,&nbsp;function을 호출한 caller에게 되돌려줄&nbsp;값을 지정합니다.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
return [[expression]]; </pre>

<dl>
 <dt><code>expression</code></dt>
 <dd>만약 return 표현식에서&nbsp;return을 생략할 경우에는&nbsp;undefined가 반환&nbsp;됩니다.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>함수에서 return statement가&nbsp;호출될 경우,&nbsp;함수는 더 이상 실행되지 않습니다. 만약 return 값이 주어진다면, 그 값이&nbsp;function을 호출한 caller에 반환됩니다. return이 생략되는 경우에는&nbsp;undefined가 반환됩니다. 아래의 return statement들은 모두 함수의 실행을 중단시킵니다.&nbsp;</p>

<pre class="brush: js">
return;
return true;
return false;
return x;
return x + y / 3;
</pre>

<h3 id="Automatic_Semicolon_Insertion">Automatic Semicolon Insertion</h3>

<p>return statement는 <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">automatic semicolon insertion (ASI)</a>의 영향을 받습니다. 줄바꿈으로는 return 키워드와 그 뒤에 나오는 표현식을 분리할 수 없습니다.</p>

<pre class="brush: js">
return
a + b;
</pre>

<p>이것을 ASI로 변형시키면 다음과 같습니다 :</p>

<pre>
return; 
a + b;
</pre>

<p>이것을 실행시키면 console은 "return statement 이후에 도달할 수 없는 코드가 있습니다"라는 경고를 띄웁니다.</p>

<div class="note">Starting with Gecko 40 {{geckoRelease(40)}}, a warning is shown in the console if unreachable code is found after a return statement.</div>

<h2 id="Examples">Examples</h2>

<h3 id="return">return</h3>

<p>아래의 함수는 x가 숫자일 때,&nbsp;x의 제곱을&nbsp;반환합니다.</p>

<pre class="brush: js">
function square(x) {
   return x * x;
}
</pre>

<h3 id="Interrupt_a_function">Interrupt a function</h3>

<p>함수는 return이 쓰여지는 지점에서&nbsp;즉시 실행을 멈춥니다.</p>

<pre class="brush: js">
function counter() {
  for (var count = 1; ; count++) {  // infinite loop
    console.log(count + "A"); // until 5
      if (count === 5) {          
        return;
      }
      console.log(count + "B");  // until 4
    }
  console.log(count + "C");  // never appears
}

counter();

// Output:
// 1A
// 1B
// 2A
// 2B
// 3A
// 3B
// 4A
// 4B
// 5A
</pre>

<h3 id="Returning_a_function">Returning a function</h3>

<p><a href="/en-US/docs/Web/JavaScript/Closures">Closures</a>에 대해서도 더 알아보세요.</p>

<pre class="brush: js">
function magic(x) {
  return function calc(x) { return x * 42 };
}

var answer = magic();
answer(1337); // 56154
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.9', 'Return statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-return-statement', 'Return statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-return-statement', 'Return statement')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope" title="En/Core_JavaScript_1.5_Reference/Functions">Functions</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Closures">Closures</a></li>
</ul>
현재 리비전 복원