Black & White Series

Literals, Variables, and Constants

JavaScript for C or Java Programmers


Atomic Literals

type typical values  
boolean true false
number 15 7.82

Strings

Basic string - two interchangeable options:

'basic string'
"basic string"

One can use one inside the other:

console.log('The dinosaur "jumped into the mud"')

Output:

The dinosaur "jumped into the mud"

Multiline String

`line 1
line 2
...`

Example:

console.log(`The dinosaur
jumped into the mud`)

Output:

The dinosaur
jumped into the mud

Automatic/Manual Conversions

console.log('2' + 3)
console.log(parseInt('2') + 3)
console.log('2' * 3)
console.log('x' - 3)

Automatic/Manual Conversions

console.log('2' + 3)
console.log(parseInt('2') + 3)
console.log('2' * 3)
console.log('x' - 3)
23
5
6
nan
  • NaN - usual output for wrong expressions

Variable/Constant Declaration

<let/const/var> <name1> = <init1>,..., <namen> = <initn>
  • let - a variable in a scope
    • equivalent behavior to C variables
  • const - a constant in a scope
  • var - old fashion variable declaration
    • rules for scope different from let

Example

let price = 20, fees = 5
const pi = 3.1416

price += fee // right
pi = 3.14 // error

References

  • Mozilla MDN - https://developer.mozilla.org

  • Eloquent JavaScript - https://eloquentjavascript.net/


André Santanchè

www.ic.unicamp.br/~santanch/

Web2Learn

santanche.github.io/web2learn/