Skip to content
Lesson 1 of 10 · JavaScript for Beginners

Your first JavaScript

Run code, print with console.log, and know the basic value types.

14 min

JavaScript is the language of the web: it makes pages react to clicks, fetches data, and, with Node.js, it also runs servers and tools. Every browser can run it, which is why you can try it here without installing anything.

You show output with console.log:

javascript2 lines
console.log("Hello, world!");
console.log(2 + 3);

Each call prints on its own line. Statements end with a semicolon (optional in many cases, but we will use them, which avoids a few surprises).

TypeExampleNotes
number42, 3.14, -7whole and decimal numbers are the same type
string"hi", 'hi', ` hi `text in quotes
booleantrue, false
undefinedundefineda variable that has no value yet
nullnull"nothing", set on purpose

typeof tells you the type of a value: typeof 3.14 is "number", typeof "3" is "string". (Quirk: typeof null is "object", a very old mistake that cannot be fixed.)

Text can use single, double or backtick quotes. Backticks make a template literal, which can hold values: ` 2 + 3 is ${2 + 3} is "2 + 3 is 5"`.

// to the end of the line and /* across several lines */ are ignored by JavaScript. Use them to explain why.

Common misconception

“JavaScript turns `"5"` into a number whenever it needs one, so `"5" + 5` is `10`.”

Subtraction does exactly that: `"5" - 2` is `3`, and the language seems to convert for you.

What is actually true

With +, if either side is a string, JavaScript glues text: "5" + 5 is "55". With -, * and / it converts to numbers. The same-looking operators behave differently, so convert on purpose with Number("5").

How to catch yourself

A result like `"55"` or `"12"` instead of `10` or `3` points to a string in your sum.

Exercise1 case
javascript

Print exactly these two lines using console.log:

code2 lines
Hello, world!
I am learning JavaScript.

Your code must satisfy

  • two lines
starter code1 line
// your console.log calls go here
Exercise1 case
javascript

Print the type of each value, one per line, using typeof: first 42, then "42", then true. The output must be:

code3 lines
number
string
boolean

Your code must satisfy

  • three types
starter code1 line
// console.log(typeof ...)
  • console.log(value) prints a value on its own line.
  • The basic types are number, string, boolean, undefined and null; typeof names the type.
  • Backtick strings (template literals) can embed values with ${...}.
  • With +, any string makes the result a string: "5" + 5 is "55".
Check yourself

Check the basics.

1Multiple choice

What does console.log(typeof "7") print?

  • Anumber
  • Bstring
  • Ctext
  • D7
2Multiple choice

What is "4" + 4?

  • A8
  • B"44"
  • C"8"
  • DNaN
3Multiple choice

What is "10" - 4?

  • A6
  • B"6"
  • C"104"
  • D"10-4"
4Select all that apply

Which of these are valid ways to write a string?

  • A`"hi"`
  • B`'hi'`
  • C`` `hi` ``
  • D`hi`
5Fill the blanks

A string with backticks that can hold ${...} is called a template .

Capitalisation, spacing and punctuation are ignored when grading.

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of JavaScript for Beginners is planned around your week. Free.