Skip to content
Educora
Beginner10 min1 / 27

What is JavaScript? Your first program

Find out what JavaScript is and where it runs, and write your first program with `console.log`.

Check yourself
In this lesson you will learn
  • Explain what JavaScript is and where it is used
  • Print text and numbers to the console with console.log
  • Write comments and recognise simple mistakes

When you tap “Like” on a social network, the heart turns red instantly and the page does not reload. You drag a map with your finger, and a search box suggests words while you are still typing. Behind all of this there is usually the same language — JavaScript.

What is JavaScript?

JavaScript is a programming language that makes web pages alive and interactive. HTML defines a page's content and CSS defines its look. JavaScript controls the page's behaviour: what happens when you click a button, whether the data in a form is valid, how new posts are loaded.

TechnologyResponsible forCompared to a house
HTMLContent and structureWalls and rooms
CSSLook: colours, fonts, layoutPaint and furniture
JavaScriptBehaviour and interactionElectricity: lights, doorbell, lift

At first JavaScript ran only in browsers. Today it is almost everywhere:

  • Websites: menus, forms, maps, online games.
  • Servers: thanks to Node.js, JavaScript also runs without a browser, on computers and servers.
  • Apps: for example, the popular code editor Visual Studio Code is built with web technologies.

Your first program: console.log

By tradition, programmers learning a new language first print “Hello, world!”. In JavaScript you use the console.log() command for this: it prints whatever is inside the parentheses to the console. Run the code below and look at the result.

JavaScript
console.log('Hello, world!');
console.log('I am learning JavaScript');
console.log(2 + 3);
▸ Expected output
Hello, world!
I am learning JavaScript
5

Each line is a separate statement, and the computer executes them one by one from top to bottom. Text in quotes — a string — is printed exactly as written, while 2 + 3 without quotes is calculated first. The semicolon ; at the end marks where a statement finishes.

Definition
Console

A window where a program prints text messages. In a browser it is part of the developer tools (DevTools); programmers use it to check their code and find mistakes.

console.log can also print several values at once: separate them with commas and they appear on one line with spaces between them. Notice the difference between '2 + 3' in quotes and 2 + 3 without quotes.

JavaScript
console.log('2 + 3');
console.log(2 + 3);
console.log('Score:', 95, 'points');
console.log('Baku', 'Ganja', 'Sumqayit');
▸ Expected output
2 + 3
5
Score: 95 points
Baku Ganja Sumqayit

Comments and typical mistakes

A comment is a note for people that the computer ignores. Everything from // to the end of the line is a comment. A comment over several lines starts with /* and ends with */. Comments help you remember later what the code does and explain it to others.

JavaScript
// This line is a comment: it is not executed
console.log('Comments are for people');

/* A comment
   over several lines */
console.log('Done'); // a comment can also stand here
▸ Expected output
Comments are for people
Done

Where can you run code?

  • In these lessons: press the run button next to an example — the code runs right in your browser.
  • In the browser console: open the developer tools and switch to the Console tab.
  • In an HTML page: put the code inside a <script> tag.
  • On your computer: install Node.js and run a file with the command node app.js.
Opens the developer tools in Chrome, Edge and FirefoxF12
Opens the console directly in Chrome and Edge (Mac: Cmd+Option+J)Ctrl+Shift+J
Opens the console in Firefox (Mac: Cmd+Option+K)Ctrl+Shift+K

In a browser, JavaScript can also change the page itself. In the example below the HTML says “Hello!” in the heading, but the script changes it as soon as the page opens. Don't worry if you don't understand every line yet — you will learn this in detail in the DOM lesson.

HTML
<h1 id="title">Hello!</h1>
<p>This paragraph comes from HTML.</p>
<script>
  document.getElementById('title').textContent = 'Hello from JavaScript!';
</script>
Preview
The script changed the heading's text — see the result in the preview.
Exercise

Print two lines with console.log: first the text Hello, Educora!, then the number of hours in a year. Don't work it out yourself — let JavaScript calculate 365 * 24.

Exercise · JavaScript
// 1) print the text Hello, Educora!
// 2) print the result of 365 * 24
▸ Expected output
Hello, Educora!
8760

Key points

  • HTML defines content, CSS defines the look, and JavaScript defines the page's behaviour.
  • JavaScript runs in the browser, and with Node.js on computers and servers.
  • console.log() prints values to the console; values separated by commas are printed with spaces.
  • // and /* */ are comments — the computer does not execute them.
  • JavaScript is case-sensitive: console.log and Console.log are different.

Check yourself

10 questions. Every correct answer earns XP.

1 / 10
What is JavaScript's main role on a web page?