Skip to content

2.16 JavaScript specials

Bunlong VAN edited this page Feb 18, 2018 · 2 revisions

This chapter briefly recaps the features of JavaScript that we’ve learned by now, paying special attention to subtle moments.

Code structure

Statements are delimited with a semicolon:

alert('Hello'); alert('World');

Usually, a line-break is also treated as a delimiter, so that would also work:

alert('Hello')
alert('World')

That’s called “automatic semicolon insertion”. Sometimes it doesn’t work, for instance:

alert("There will be an error after this message")

[1, 2].forEach(alert)

Most codestyle guides agree that we should put a semicolon after each statement.

Semicolons are not required after code blocks {...} and syntax constructs with them like loops:

function f() {
  // no semicolon needed after function declaration
}

for(;;) {
  // no semicolon needed after the loop
}

... But even if we can put an “extra” semicolon somewhere, that’s not an error. It will be ignored.

More in: Code structure.

Strict mode

To fully enable all features of modern JavaScript, we should start scripts with "use strict".

'use strict';

...

The directive must be at the top of a script or at the beginning of a function.

Without "use strict", everything still works, but some features behave in the old-fashion, “compatible” way. We’d generally prefer the modern behavior.

Some modern features of the language (like classes that we’ll study in the future) enable strict mode implicitly.

More in: The modern mode, "use strict".

Variables

Can be declared using:

  • let

  • const (constant, can’t be changed)

  • var (old-style, will see later)

A variable name can include:

  • Letters and digits, but the first character may not be a digit.

  • Characters $ and _ are normal, on par with letters.

  • Non-Latin alphabets and hieroglyphs are also allowed, but commonly not used.

Variables are dynamically typed. They can store any value:

let x = 5;
x = "John";

There are 7 data types:

  • number for both floating-point and integer numbers,

  • string for strings,

  • boolean for logical values: true/false,

  • null – a type with a single value null, meaning “empty” or “does not exist”,

  • undefined – a type with a single value undefined, meaning “not assigned”,

  • object and symbol – for complex data structures and unique identifiers, we haven’t learnt them yet.

The typeof operator returns the type for a value, with two exceptions:

typeof null == "object" // error in the language
typeof function(){} == "function" // functions are treated specially

More in: Variables and Data types.

Interaction

We’re using a browser as a working environment, so basic UI functions will be:

prompt(question[, default])

Ask a question, and return either what the visitor entered or null if he pressed “cancel”.

confirm(question)

Ask a question and suggest to choose between Ok and Cancel. The choice is returned as true/false.

alert(message)

Output a message.

All these functions are modal, they pause the code execution and prevent the visitor from interacting with the page until he answers.

For instance:

let userName = prompt("Your name?", "Alice");
let isTeaWanted = confirm("Do you want some tea?");

alert( "Visitor: " + userName ); // Alice
alert( "Tea wanted: " + isTeaWanted ); // true

More in: Interaction: alert, prompt, confirm.

Operators

JavaScript supports the following operators:

Arithmetical

Regular: * + - /, also % for the remainder and ** for power of a number.

Binary plus + concatenates strings. And if any of the operands is a string, the other one is converted to string too:

alert( '1' + 2 ); // '12', string
alert( 1 + '2' ); // '12', string
Clone this wiki locally