Variáveis
Tipos de dados
JavaScript for impatient programmers (Book)
Category | Types | Values |
---|---|---|
Primitive | Undefined | undefined |
Primitive | Null | null |
Primitive | Boolean | true , false |
Primitive | Number | -15 15 , 0b1111 , 0o17 , 0xf -123.45 123.45 , 1.2345e2 , 12345E-2 |
Primitive | String | 'Hello' , "Hello" , `Hello` |
Object | Array | [] [1, 2, 3] [1, '2', true, [3, false]] ["Alice", "alice@email"] |
Object | Object | {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;