Alert, Prompt, and Console in JavaScript
JavaScript provides built-in functions to interact with users and developers. These include alert for pop-up messages, prompt for user input, and console for logging and debugging.
๐ 1. alert()
Displays a popup message to the user. It's commonly used for notifications or warnings.
alert("Welcome to JavaScript!");
โ ๏ธ The alert box blocks further interaction until dismissed.
๐งพ 2. prompt()
Asks the user to input a value and returns that value as a string.
let name = prompt("What is your name?");
alert("Hello, " + name);
๐ If the user clicks "Cancel",
prompt()
returns null
.
๐ 3. console.log()
Outputs data to the browser's developer console. Useful for debugging and inspecting values.
console.log("Page loaded");
let x = 5;
console.log("Value of x is:", x);
๐ 4. Other Console Methods
console.warn("Warning message")
โ Shows a yellow warningconsole.error("Error message")
โ Shows a red error messageconsole.table(object)
โ Displays data as a table
let user = { name: "Alice", age: 30 };
console.table(user);
๐ฏ Summary
alert()
โ Shows popup messageprompt()
โ Gets user inputconsole.log()
โ Prints to the developer console
โ
Tip: Use
console.log()
often to test code logic and track variable values.