JavaScript Alerts - Adding Behaviour to Websites

 Getting Started with JavaScript in Chrome Console & Snippets


1️⃣ Opening the Console and Writing JS Code
  • Open a new tab in Chrome.

  • Go to Menu → View → Developer → JavaScript Console or ( Ctrl + Shift + j ).

  • The Console allows you to write JavaScript code directly and run it instantly by pressing Enter.

Example:

alert("Hello");
  • Press Enter and a pop-up will appear showing “Hello”.

  • Click OK to close the pop-up.


2️⃣ Writing Multi-Line Code

  • If you want multiple alerts one after another:

alert("Hello"); alert("World");
  • To write multi-line code in the console, hold Shift + Enter to go to the next line without running the previous one.


3️⃣ Using Snippets (For Running Larger Scripts)

  • Open DevTools → Sources → left corner arrow → Snippets.

  • Click + New snippet to create a new JS file (e.g., index.js).

  • You can write as many lines of code as you want here.

  • Once done, click Run to execute the entire script at once.

Example:

alert("Hello!"); alert("World");
  • Running this will show a “Hello” pop-up first, then “World”.


4️⃣ Which Instructions Work?

  • The browser only recognizes valid JavaScript keywords.

  • Example: say("Hello") will not work because say is not a JS keyword.

  • Error message: Uncaught ReferenceError: say is not defined

💡 Meaning: Use only recognized JS commands.


6️⃣ JS Grammar / Syntax

  • Semicolon (;) → Marks the end of a statement.

  • Quotation marks (" " / ' ') → Denotes a string or text.

  • Parentheses ( ) → Used to pass arguments to a function or method.

  • Keywords → Commands that tell the browser what to do (e.g., alert).

Tip:

  • Do not use quotes from Word or Google Docs—they can cause errors.

  • Always type code in a proper code editor or Snippet tool for correct symbols.


7️⃣ Stylistic Best Practices

  • Double quotes " " are commonly used; single quotes ' ' are used in some cases.

  • Consistent code formatting makes your code readable for others.


Summary

  • Console → Use for testing small snippets.

  • Snippets → Use for multi-line or complete scripts.

  • Keywords, semicolons, parentheses, quotes → Fundamental JS syntax.

  • Best practices → Keep code readable and uniform.

Comments