Variáveis

Tipos de dados

JavaScript's type hierarchy

JavaScript for impatient programmers (Book)

CategoryTypesValues
PrimitiveUndefinedundefined
PrimitiveNullnull
PrimitiveBooleantrue, false
PrimitiveNumber-15
15, 0b1111, 0o17, 0xf
-123.45
123.45, 1.2345e2, 12345E-2
PrimitiveString'Hello', "Hello", `Hello`
ObjectArray[]
[1, 2, 3]
[1, '2', true, [3, false]]
["Alice", "alice@email"]
ObjectObject{name: "Alice", email: "alice@email"}

Declaração de variáveis

Linguagem C:

int number = 10;
int number = 10;

Linguagem Python:

number = 10
number = 10

Keyword

var number = 10;
console.log(number); //=> 10
var number = 10;
console.log(number); //=> 10
let number = 10;
console.log(number); //=> 10
let number = 10;
console.log(number); //=> 10
const number = 10;
console.log(number); //=> 10
const number = 10;
console.log(number); //=> 10

Reassociação

var number;
number = 10;
number = 100;
console.log(number); //=> 100
var number;
number = 10;
number = 100;
console.log(number); //=> 100
let number;
number = 10;
number = 100;
console.log(number); //=> 100
let number;
number = 10;
number = 100;
console.log(number); //=> 100
const number; // SyntaxError
const number = 10;
number = 100; // TypeError
const number; // SyntaxError
const number = 10;
number = 100; // TypeError
const numbers = [];
numbers.push(1);
console.log(numbers); //=> [ 1 ]
const numbers = [];
numbers.push(1);
console.log(numbers); //=> [ 1 ]

Escopo

var number = 10;
var number = 10;
var number = 10;
var number = 10;
let number = 10;
let number = 10; // SyntaxError: Identifier 'number' has already been declared
let number = 10;
let number = 10; // SyntaxError: Identifier 'number' has already been declared
const number = 10;
const number = 10; // SyntaxError: Identifier 'number' has already been declared
const number = 10;
const number = 10; // SyntaxError: Identifier 'number' has already been declared

Tipagem Dinâmica

let variable = 10;
console.log(variable); //=> 10
console.log(typeof variable); //=> number
 
variable = 'fulano';
console.log(variable); //=> 'fulano'
console.log(typeof variable); //=> string
let variable = 10;
console.log(variable); //=> 10
console.log(typeof variable); //=> number
 
variable = 'fulano';
console.log(variable); //=> 'fulano'
console.log(typeof variable); //=> string

Tipagem Fraca

const x = 10;
const y = '5';
console.log(x - y); //=> 5
const x = 10;
const y = '5';
console.log(x - y); //=> 5

Case Sensitive

const number = 8;
const Number = 80;
const NUMBER = 800;
const number = 8;
const Number = 80;
const NUMBER = 800;

Editar esta página