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:
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).
| Type | Example | Notes |
|---|---|---|
| number | 42, 3.14, -7 | whole and decimal numbers are the same type |
| string | "hi", 'hi', ` hi ` | text in quotes |
| boolean | true, false | |
| undefined | undefined | a variable that has no value yet |
| null | null | "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.
“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.
Print exactly these two lines using console.log:
Hello, world!
I am learning JavaScript.Your code must satisfy
- two lines
// your console.log calls go herePrint the type of each value, one per line, using typeof: first 42, then "42", then true. The output must be:
number
string
booleanYour code must satisfy
- three types
// console.log(typeof ...)console.log(value)prints a value on its own line.- The basic types are number, string, boolean, undefined and null;
typeofnames the type. - Backtick strings (template literals) can embed values with
${...}. - With
+, any string makes the result a string:"5" + 5is"55".
Check the basics.
What does console.log(typeof "7") print?
- Anumber
- Bstring
- Ctext
- D7
What is "4" + 4?
- A8
- B"44"
- C"8"
- DNaN
What is "10" - 4?
- A6
- B"6"
- C"104"
- D"10-4"
Which of these are valid ways to write a string?
- A`"hi"`
- B`'hi'`
- C`` `hi` ``
- D`hi`
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.