This page was translated from English by the community.Learn more and join the MDN Web Docs community.
return
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
return 명령문은 함수 실행을 종료하고, 주어진 값을 함수 호출 지점으로 반환합니다.
In this article
시도해 보기
function getRectArea(width, height) { if (width > 0 && height > 0) { return width * height; } return 0;}console.log(getRectArea(3, 4));// Expected output: 12console.log(getRectArea(-3, 4));// Expected output: 0구문
return [[expression]];expression반환할 값으로 사용할 표현식. 생략할 경우
undefined를 대신 반환합니다.
설명
함수 본문에서return 명령문에 도달하면 함수의 실행은 그 지점에서 중단됩니다. 값을 제공한 경우 함수를 호출한 곳에 그 값을 반환합니다. 예를 들어, 다음 함수는 숫자 매개변수x의 제곱을 반환합니다.
function square(x) { return x * x;}var demo = square(3);// demo는 9값을 명시하지 않으면 대신undefined를 반환합니다.
다음return 명령문 모두 함수 실행을 끊습니다.
return;return true;return false;return x;return x + y / 3;자동 세미콜론 삽입
return 명령문은자동 세미콜론 삽입(ASI)의 영향을 받습니다.return 키워드와 표현식 사이에는 줄바꿈 문자가 올 수 없습니다.
returna + b;위 코드는 ASI로 인해 아래처럼 처리됩니다.
return;a + b;콘솔이 "unreachable code after return statement" 경고를 출력할 것입니다.
문제를 해결하려면 괄호를 사용해 ASI를 방지해야 합니다.
return a + b;예제
>함수 중단
함수는return을 호출하는 지점에서 즉시 실행을 멈춥니다.
function counter() { for (var count = 1; ; count++) { // 무한 반복 console.log(count + "A"); // 5까지 if (count === 5) { return; } console.log(count + "B"); // 4까지 } console.log(count + "C"); // 절대 나타나지 않음}counter();// 출력:// 1A// 1B// 2A// 2B// 3A// 3B// 4A// 4B// 5A함수 반환하기
클로저에 대해서도 더 알아보세요.
function magic(x) { return function calc(x) { return x * 42; };}var answer = magic();answer(1337); // 56154명세서
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-return-statement> |