Learn JavaScript
JavaScript is the programming language that brings web pages to life, turning static documents into interactive applications.
What Is JavaScript?
JavaScript is a high-level programming language that runs inside every modern web browser. Alongside HTML and CSS, it forms the third core technology of the web: HTML describes the structure of a page, CSS controls how it looks, and JavaScript decides how it behaves. When you click a button and something happens, when a form warns you about a missing field, or when new content appears without the page reloading, JavaScript is almost always the reason.
Although it was born in the browser, JavaScript has since grown far beyond it. With runtimes like Node.js, the same language now powers web servers, command-line tools, mobile apps, and even desktop software. Learning it once gives you a foothold in an enormous range of projects.
Why Learn JavaScript?
- It is the only language browsers understand natively, so every interactive website relies on it.
- It is beginner-friendly: you can write your first line of code without installing anything.
- It runs everywhere, from front-end interfaces to back-end servers, thanks to a huge ecosystem.
- It has one of the largest developer communities, which means abundant tutorials, libraries, and help.
Your First Lines of JavaScript
The classic starting point is printing a message. The console.log() function displays a value in the browser's developer console, which is a handy place to inspect what your code is doing.
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Introduction</h2>
<script>
// Print a friendly greeting to the console
console.log('Hello, world!');
// You can log numbers and calculations too
console.log(5 + 7);
</script>
</body>
</html>JavaScript can also change the content of a web page directly. In the example below, the code finds an element on the page and replaces its text, which is the essence of what makes pages interactive.
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Introduction</h2>
<p id="demo">Original text</p>
<script>
// Replace the text inside an element with the id "demo"
document.getElementById('demo').innerHTML = 'JavaScript changed this text!';
</script>
</body>
</html>What You Can Build
Once you are comfortable with the basics, JavaScript lets you validate forms before they are sent, animate elements, fetch data from servers without refreshing the page, build entire single-page applications, and much more. This tutorial starts from zero and builds up those skills one topic at a time.
Exercise: JavaScript Introduction
What is JavaScript primarily used for on a webpage?
Frequently Asked Questions
- Is JavaScript the same as Java?
- No. They are unrelated languages with similar names, a marketing decision from the 1990s. JavaScript runs in browsers and on Node.js and is dynamically typed; Java is a separate compiled language used mainly for backend and Android work.
- Do I need to learn HTML and CSS before JavaScript?
- Yes, at least the basics. JavaScript's main job in a browser is changing HTML elements and CSS styles, so without them there is nothing for your code to act on.
- How long does it take to learn JavaScript?
- Around three months of steady practice for the fundamentals, and six to twelve months to be comfortable with a framework and asynchronous code. The language is small; the ecosystem around it is what takes time.
- What is the difference between JavaScript and TypeScript?
- TypeScript is JavaScript plus a type system. You write the same language with type annotations, and it compiles to plain JavaScript. Types catch mistakes before the code runs, at the cost of a build step.