Javascript datatypes: Difference between revisions
(→Arrays) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 43: | Line 43: | ||
</source> | </source> | ||
</blockquote><!-- Literals --> | </blockquote><!-- Literals --> | ||
= Type Conversion = | |||
<blockquote> | |||
<syntaxhighlight lang="javascript"> | |||
Number("123"); | |||
String(123); | |||
Array.from(document.querySelectAll("li")); | |||
</syntaxhighlight> | |||
</blockquote><!-- Type Conversion --> | |||
= Primitive Types = | = Primitive Types = | ||
Line 84: | Line 94: | ||
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number | ||
Numbers smaller than <code>Number.MAX_SAFE_INTEGER</code> | Numbers must be smaller than <code>Number.MAX_SAFE_INTEGER</code> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Number("1") | Number("1") // convert to number | ||
Number("abc") | Number("abc") // returns NaN if unable to convert | ||
123 // | |||
123.0 // | |||
0o755 0755 // 755 in octal | |||
parseInt('101', 2) // binary to decimal | |||
parseInt('644', 8) // octal to decimal | |||
</source> | </source> | ||
</blockquote><!-- Numbers --> | </blockquote><!-- Numbers --> | ||
Line 134: | Line 149: | ||
BigUint64Array | BigUint64Array | ||
</source> | </source> | ||
<syntaxhighlight lang="javascript"> | |||
let animals = ["cat", "dog", "monkey", "bird"]; | |||
animals.slice(2, 4) // ["monkey", "bird"] | |||
animals.filter((i) => { return i.includes("o") }) // ["dog", "monkey"] | |||
animals.map(x => "--" + x) // ["--cat", "--dog", ..] | |||
animals.map(myFunction()) // | |||
animals.shift() // "cat" | |||
animals.pop() // "bird" | |||
</syntaxhighlight> | |||
</blockquote><!-- Arrays --> | </blockquote><!-- Arrays --> | ||
Latest revision as of 12:14, 5 February 2023
Documentation
MDN: builtin datatypes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
Literal Types
Literals are types that have syntax outlined for them.
// ==== // text // ==== 'foobar' // string /[0-9]+/ // regexp // ========== // templating // ========== `hello ${name}` `multiline string` // ======= // numbers // ======= true, false // boolean 123 // number 123.123 // float 0o77, 0O77, 077 // octal number Ox1123 // hex number 0b0011 // binary number ['one', two'] // array {a: 1, b: 2} // object literal
Type Conversion
Number("123"); String(123); Array.from(document.querySelectAll("li"));
Primitive Types
undefined
All declared variables are undefined until they are assigned a value.
undefined
null
null
String
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
- strings are unicode
'hello'.length; // 5 'foo bar'.replace('bar', 'baz') // 'foo baz'Symbol
https://developer.mozilla.org/en-US/docs/Glossary/Symbol
Essentially a global immutable string.
Numbers
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
Numbers must be smaller than
Number.MAX_SAFE_INTEGER
Number("1") // convert to number Number("abc") // returns NaN if unable to convert 123 // 123.0 // 0o755 0755 // 755 in octal parseInt('101', 2) // binary to decimal parseInt('644', 8) // octal to decimalBigInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#bigint_type
Arbitrary precision numbers. Numbers larger than
Number.MAX_SAFE_INTEGER
.Boolean
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
// false Boolean('') Boolean(0) // true
Collections
Arrays
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
There are a few typed variations of Arrays
Array // Integer Arrays -- 8/16/32 bit variations Int8Array Uint8Array // Float Arrays Float32Array Float64Array // BigInt Arrays BigInt64Array BigUint64Arraylet animals = ["cat", "dog", "monkey", "bird"]; animals.slice(2, 4) // ["monkey", "bird"] animals.filter((i) => { return i.includes("o") }) // ["dog", "monkey"] animals.map(x => "--" + x) // ["--cat", "--dog", ..] animals.map(myFunction()) // animals.shift() // "cat" animals.pop() // "bird"Sets
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
Objects
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects
Objects are the superclass of most objects in javascript.
It behaves similarly to hashes, hashmaps, dictionaries in other languages except you can also bind methods to it.// emtpy object var obj = {} var obj = new Object() obj['key'] = 'value'See much more in javascript objects.
Maps
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
Similar to objects, but ordered, iterable, and keys can be any datatype.
let students = new Map() let students = new Map([[1, 'one'], [2, 'two']]) let students = new Map(students) students.set('Alex', 1) students.get('Alex') // 1 students.has('Alex') // true/false students.size // length, count, number of elementsIteration
for (let [key, val] of students) { ... } students.forEach(function(val, key) { ... })
Complex Types
Date
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Javascript dates contain both dates and time.
new Date(1970, 1, 1) new Date(628021800000) // epoch timestamp Date.now() Date.UTC()