-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
while
Loop – Repeat While a Condition is TrueThe while
loop continues to execute a block of code as long as the specified condition evaluates to true.
while (condition) {
// code block to be executed
}
condition is evaluated before each iteration.
If it returns true
, the code inside the loop runs.
When the condition becomes false
, the loop stops.
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
// Output: 1 2 3 4 5
If you forget to update the loop variable (like i++
), the condition may never become false, causing an infinite loop.
let i = 1;
while (i <= 5) {
console.log(i);
// missing i++
}
🚨 Always ensure your loop has a way to exit.
break
and continue
let i = 1;
while (i <= 5) {
if (i === 3) {
i++;
continue;
}
console.log(i);
i++;
}
// Output: 1 2 4 5
let j = 1;
while (j <= 5) {
if (j === 4) break;
console.log(j);
j++;
}
// Output: 1 2 3
Q1. What is the syntax of a while
loop in JavaScript?
Q2. How do you use a while
loop to print numbers from 1 to 10?
Q3. How can you avoid an infinite while
loop in your code?
Q4. What happens if the condition in a while
loop is always false from the beginning?
Q5. How can you use break
to exit a while
loop early?
Q6. How do you use continue
to skip a particular iteration in a while
loop?
Q7. Write a while
loop that prints only even numbers between 1 and 10.
Q8. How do you count backward using a while
loop?
Q9. Can a while
loop run even once if the condition is false at the beginning? Explain.
Q10. Write a while
loop that stops when a variable reaches a randomly generated number.
Q1: What is checked before each iteration in a while loop?
Q2: What happens if the condition in a while loop is always true?
Q3: Which of the following is the correct syntax for a while loop?
Q4: Which of these will create an infinite loop?
Q5: How do you exit a while loop early?
Q6: Which of the following correctly prints even numbers from 2 to 10 using a while loop?
Q7: Which loop checks the condition before running the loop body?
Q8: Which keyword is used to skip the current iteration and continue with the next in a while loop?