JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

michelle Jia
michelle Jia
4,617 Points

break statement not work in my program, check error in the console, saying illegal break statement, please kindly advise

function store (){ search = prompt('search a product in our store, or you can input list to show all the products, or you can input quit to exit'); search= search.toLowCase(); if (search === 'quit'){ break; }else if (search === 'list'){ print(inStock.join('; ')); }else { if (inStock.indexOf(search)>-1){ print('Yes, we have the ' + search + ' in our store.' ); }else { print('Sorry, we don't have the ' + search + '.'); } } }

1 Answer

Steven Parker
Steven Parker
173,976 Points

A "break" is used to end a loop, and is only allowed inside a loop. Did you intend to create a loop around this code?

If you want to end a function, use "return" instead.