SmartCodingTips

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 warning
  • console.error("Error message") โ€“ Shows a red error message
  • console.table(object) โ€“ Displays data as a table
let user = { name: "Alice", age: 30 };
console.table(user);

๐ŸŽฏ Summary

  • alert() โ€“ Shows popup message
  • prompt() โ€“ Gets user input
  • console.log() โ€“ Prints to the developer console
โœ… Tip: Use console.log() often to test code logic and track variable values.