개발하고 싶은 초심자
[Error] Jump target cannot cross function boundary 본문
기술개념정리(in Javascript)/개발&에러핸들링(in Work(Codestates))
[Error] Jump target cannot cross function boundary
정새얀 2023. 1. 9. 16:27해당 글을 작성하다가 나온 에러 메시지인데, if문 안에 return 대신 continue를 작성했을 때 발생했다.
Jump target cannot cross function boundary.
직역하면 점프 대상은 함수 경계를 넘을 수 없습니다, 라는 의미인데,
이는 when we try to use a break/continue statement outside of a for loop, e.g. inforEach()or a function,
즉 forEach()나 함수 등 for loop 바깥쪽에 break/continue를 사용하려고 할 때 발생하는 오류인 것이다.
이를 해결할 수 있는 방법은 두 가지가 있는데
① try ... catch문을 사용한다.
// ex) try catch 예제
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const maxValue = 4;
const errorMaxLimit = "Max limit reached";
try {
myArray.forEach((value) => {
if (value > maxValue) throw new Error(errorMaxLimit);
console.log('Current value is ', value);
});
} catch(error) {
if (!error.property || error.property !== errorMaxLimit) {
// another error has happened
throw error;
}
}
② return을 사용하여 원하지 않는 로직이 실행되지 않도록 한다.
// ex) return 예제
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const maxValue = 4;
myArray.forEach((value) => {
if (value > maxValue) return;
// use the "return" keyword instead of the "break/continue" keyword
console.log('Current value is ', value);
});
'기술개념정리(in Javascript) > 개발&에러핸들링(in Work(Codestates))' 카테고리의 다른 글
[React] Uncaught Error: useRoutes() may be used only in the context of a <Router> component. (0) | 2023.02.18 |
---|---|
[React] 배열에서 인덱스가 밀리는 이유 (0) | 2023.01.11 |
[DOM] 자식요소 중 일부를 제외하고 삭제하는 방법 (0) | 2023.01.09 |
[HTTP Method] PUT vs PATCH (0) | 2022.12.21 |
[React] useState는 비동기인가 동기인가 (0) | 2022.12.20 |
Comments