break
로 반복문 종료하고 반복문 다음에 있는 코드로 프로그램 진행시킬 수 있음.
let products = [
{ name: 'phone', price: 700 },
{ name: 'tablet', price: 900 },
{ name: 'laptop', price: 1200 }
];
// let 안쓰고 var사용한거는 밑에서 i값을 보여주기 위함임.
for (var i = 0; i < products.length; i++) {
if (products[i].price == 900)
break;
}
// show the products
console.log(products[i]);
Output:
{ name: 'tablet', price: 900 }
아래는 break
로 switch
문 나가는 예시임.
let products = [
{ name: 'phone', price: 700 },
{ name: 'tablet', price: 900 },
{ name: 'laptop', price: 1200 }
];
let discount = 0;
let product = products[1];
switch (product.name) {
case 'phone':
discount = 5;
break;
case 'tablet':
discount = 10;
break;
case 'laptop':
discount = 15;
break;
}
console.log(`There is a ${discount}% on ${product.name}.`);
Output:
There is a 10% on tablet.
break
로 반복문이나 switch 종료가능