Dia 47/2019 - Estruturas de dados em JS: tipos de dados

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 47, mostrando o uso dos tipos de dados com javascript.

Código

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Primitives
**********************/
// string
const string = '';
console.log(typeof string);

// number
const number = 1;
console.log(typeof number);
console.log(typeof NaN, '<<< NaN');
console.log(typeof Infinity, '<<< Infinity');

// boolean
const boolean = true;
console.log(typeof boolean);

// null
console.log(typeof null, '<<< null'); // "object" (not "null" for legacy reasons)
// In the first implementation of JavaScript, values were represented in two parts - a type tag and the actual value.
// There were 5 type tags that could be used, and the tag for referencing an object was 0.
// The null value, however, was represented as the NULL pointer, which was 0x00 for most platforms.
// As a result of this similarity, null has the 0 type tag, which corresponds to an object.

// undefined
let und;
console.log(typeof und, typeof undefined);

// Symbol
const symbol = Symbol('foo');
console.log(typeof symbol);

// BigInt
// https://www.npmjs.com/package/jsbi
const bigint = 1n;
console.log(typeof bigint);

/**
* Objects
**********************/
const object = new Object();
console.log(typeof object, object.constructor);
const array = new Array('1', '2');
console.log(typeof array, array.constructor);
const objNumber = new Number(4);
console.log(typeof objNumber, objNumber.constructor);
const objString = new String('abc');
console.log(typeof objString, objString.constructor);
const objBoolean = new Boolean(true);
console.log(typeof objBoolean, objBoolean.constructor);
const objDate = new Date();
console.log(typeof objDate, objDate.constructor);
const objFunction = new Function;
console.log(typeof objFunction, objFunction.constructor);
// Functions belong to the object type. But typeof treats them differently, returning "function". That’s not quite correct, but very convenient in practice.
const objSymbol = Symbol('a');
console.log(typeof new Object(objSymbol), objSymbol.constructor); // new Symbol() === TypeError
const objBigInt = BigInt(1n);
console.log(typeof new Object(objBigInt), objBigInt.constructor); // new BigInt() === TypeError

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Relacionados

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.