JavaScript: Getting Started with JavaScript

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: Dana🏷 Category: JavaScript Tutorial

Adding JavaScript to your web page is easier than you might think — there are three ways to do it, from quick-and-dirty to production-ready.

Related BOM Methods:alert()

1. Three Ways to Add JavaScript

(1) Inline (onclick attribute)

Write JS code directly in an HTML element's event attribute, like onclick="...".

This is the simplest approach, but don't use it in real projects — mixing HTML and JS together makes maintenance a nightmare.

(2) Internal (<script> tag)

Insert a <script> tag in your HTML file and write JS code inside it. This works well for small pages and is the most common approach during learning and testing.

The <script> tag can go in <head> or <body>, but placing it right before </body> is recommended so it doesn't block page rendering.

(3) External (.js file)

Write your JS code in a separate .js file and include it with <script src="xxx.js"></script>. This is the standard practice for real projects — it separates concerns, enables reuse, and lets the browser cache the file for faster loading.

▶ Example: Inline JavaScript

▶ Try it Yourself

▶ Example: Internal JavaScript

▶ Try it Yourself

▶ Example: External JavaScript

▶ Try it Yourself

If you're using VS Code (highly recommended), these two extensions are all you need:


3. Running JS in Chrome DevTools

Press F12 to open DevTools, switch to the Console panel, and you can type JS code directly and press Enter to run it. This is the fastest way to experiment — test any code snippet without creating an HTML file.

Tip: Press the ↑ key in the Console to recall your previous command, saving you from retyping.


4. The defer and async Attributes on script Tags

When a <script> tag is in the <head>, the browser pauses HTML parsing to download and execute the JS first — which can make the page feel stuck. The defer and async attributes solve this:

Attribute Behavior
None Download and execute immediately, blocks HTML parsing
defer Download in background, execute after HTML is fully parsed (in order)
async Download in background, execute as soon as downloaded (order not guaranteed)

Best practice: adding defer to external JS files is the safest choice — <script src="app.js" defer></script>.


Related BOM Methods:alert()

❓ FAQ

Q What is the difference between JavaScript and Java?
A They are completely different languages that only happen to have similar names. JavaScript is mainly used for web page interactivity and runs in the browser, while Java is a general-purpose programming language often used for backends and enterprise systems. You don't need any Java background to take this course.
Q Why does my JS code throw an error saying it can't find a DOM element?
A This usually happens because the <script> is placed in the head, so the HTML below it hasn't been parsed yet when the script runs. Move the script to the bottom of the body, or add the defer attribute to the <script>, so it executes after the DOM is ready.
Q Should JavaScript code go inside the HTML or in a separate .js file?
A When learning or debugging small snippets, you can write it directly inside a <script> tag, but for real projects it's better to put it in a separate .js file and include it with <script src="...">. This makes it easier to reuse, cache, and maintain, and it also keeps the HTML smaller.

📖 Summary

📝 Exercises

  1. Beginner: Create an HTML page with an internal <script> that shows an alert saying "Welcome to JavaScript!" when the page loads (hint: use alert()).
  2. Intermediate: Create an HTML page and a separate main.js file. Include the JS file with <script src="main.js" defer></script> and implement a button that changes the page background color.
  3. Challenge: Create a page with three buttons, each representing one of the three inclusion methods (inline, internal, external). When any button is clicked, display "This JS was loaded via XXX method" on the page, using different colors to distinguish them.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏