← 講座に戻る: JavaScript from scratch

What JavaScript is and your first program

動画は近日公開

JavaScript is a programming language that was created in 1995 to bring web pages to life. Today it is much more: it runs in the browser, on servers thanks to Node.js, in desktop applications and even on phones. If you have seen a menu that opens, a form that warns you about a mistake without reloading the page, or a map that moves, it was almost certainly JavaScript. In this course you will learn the modern version of the language (ES2024), which is the one used in real projects.

Where your code runs

A program is a list of instructions that an engine reads and runs from top to bottom. Browsers include that engine (Chrome uses V8, Firefox uses SpiderMonkey) and Node.js uses the same V8 outside the browser. The important difference is the environment: in the browser you have the page (the DOM) and the window; in Node.js you have files, the network and the operating system. The language is the same; what changes are the tools available.

Option 1: the browser console

The fastest way to start needs no installation. Open your browser, press F12 (or right-click and choose Inspect) and go to the Console tab. There you can type an instruction, press Enter and see the result immediately. It is perfect for trying small ideas. Type the following and check that the message appears.

javascript
console.log('Hello, world!');
console.log(2 + 3);

Option 2: Node.js and a file

For longer programs it is better to save the code in a file. Install Node.js 22 LTS from nodejs.org, create a folder, put a file called hello.js inside it with the code below and run it from the terminal with the command node hello.js. Every time you change the file, save it and run it again. An editor such as Visual Studio Code will help you with colors and warnings.

javascript
// hello.js
console.log('My first program');
console.log('Line two runs after line one');
console.log('Sum:', 10 + 5);
// Output:
// My first program
// Line two runs after line one
// Sum: 15

Comments and common mistakes

Everything after // on a line is a comment: JavaScript ignores it and it explains the code to whoever reads it, including you a month from now. The most typical beginner mistakes are forgetting to close a quote or a parenthesis, writing console.Log with a capital letter (JavaScript is case sensitive) and running a file from the wrong folder. When you see a red error, read all of it: it almost always tells you the file and the line of the problem.

  • JavaScript runs in browsers and in Node.js.
  • The console is ideal for trying things; .js files are for complete programs.
  • console.log prints values to the screen.
  • // starts a comment and uppercase and lowercase matter.

✎ マイノート

このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字

次へ →