about
Bits JavaScript Data
08/01/2024
Bits: JavaScript Data Types
types, initialization, and construction
0
Synopsis:
This page demonstrates simple uses of the most important JavaScript types. The purpose is to quickly acquire some familiarity with types and their uses.- JavaScript provides only dynamic reference types.
- Creation of a dynamic type creates a handle that points to an instance in the managed heap.
- Assignment of dynamic types assigns handles to instances on the managed heap, resulting in two handles to the same instance.
- A variable bound to a JavaScript instance can be rebound, at run-time, to an instance of any other dynamic type.
JavaScript Types Details
Table 1.0 JavaScript Types
Type | Comment | Example |
---|---|---|
-- Primitive reference types ---- | ||
ints and floats | ||
arbitrarily large integer up to capacity of memory | ||
immutable sequence of chars change creates new String object |
|
|
exactly two values, |
||
unique value which may be used as key for object structures | ||
value given to unintialized variable | ||
|
|
|
-- Object reference types ---- | ||
Structure with hashed (key, value) elements |
o.name = "Jim" o.show = function () { document.write("name = ", o.name); } |
|
Object with reference to code in static memory so it can be executed |
/* executes using a */ } |
|
sequential resizeable indexed collection of arbitrary elements |
let b = new Array(1, "three", 2); |
|
holds a timestamp equal to number of milliseconds since January 1, 1970, UTC, and provides methods to access current local time and UTC, and set timestamps |
let day_of_week = dt.getDay(); |
|
|
holds state machine that matches an input string to a specified pattern Regular Expressions Reference |
let lines = text.split(/\r\n|\r|\n); // split accepts RegEx |
Error objects are thrown when runtime error occurs. Error subtypes: |
|
|
Many additional JavaScript defined types: | Map, Set, Int32Array, DataView, Atomics, ..., Math, ..., | |
-- User-defined Types -- | ||
User-defined types | Based on classes, these will be discussed in the next Bit. |
JavaScript Type Attributes
Table 2. JavaScript Type System Attributes
Dynamic typing | JavaScript types are dynamic. All names bind to any type, types are evaluated at run-time, and names may be rebound to any type of data. All data is held in the JavaScript managed heap. |
Type inference | Compiler infers all types from the declaring definition at load or run-time. |
Duck typing | All expressions are checked at run-time. Exceptions are thrown at load-time if there are any failures of syntax. An exception is thrown at run-time if an expression cannot be evaluated. |
Generics | There are no generics in JavaScript code since any type can be passed to a function or added to a collection. The JavaScript interpreter checks the validity of operations on data and throws exceptions if expressions are invalid. |
1.0 JavaScript Types
1.1 Demonstration Code
<!DOCTYPE html>>
<html>
<!--
Js_Data.html
JavaScript types:
Number, BigInt, String, Boolean,
null, undefined,
Object, Symbol
-->
<head>
<script>
var nl = "<br />";
var nil = "";
/* Demonstration starts here */
function heading() {
document.getElementById("data").innerHTML =
"<h2>Javascript Data</h2>";
}
function execute() {
showNote("Demo numbers")
println("-- let t1 = 42 --");
let t1 = 42;
showType(t1, "t1");
println(nil);
println("-- let t2 = t1 --");
let t2 = t1;
isSameObject(t2, "t2", t1, "t1");
println(nil);
println(
"-- let t2 = 3.1415927 -- : change of object"
);
t2 = 3.1415927;
isSameObject(t2, "t2", t1, "t1");
print(nl);
showType(t1, "t1");
showType(t2, "t2");
println(nil);
showNote("Demo strings");
println("-- let t3 = \"a string\" --");
let t3 = "a string";
showType(t3, "t3");
print(nl);
println("-- let t4 = t3 --");
let t4 = t3;
isSameObject(t4, "t4", t3, "t3");
println(nil);
showType(t3, "t3");
showType(t4, "t4");
print(nl);
println(
"-- t4 += " +
"\" and another string\" -- : copy on write"
);
t4 += " and another string";
isSameObject(t4, "t4", t3, "t3");
println(nil);
showType(t3, "t3");
showType(t4, "t4");
print(nl);
showNote("Demo arrays");
println("--- t5 = [1, 2, 3] ---");
let t5 = [1, 2, 3];
showType(t5, "t5");
print(nl);
println("--- let t6 = t5 ---");
let t6 = t5;
isSameObject(t6, "t6", t5, "t5");
println(nil);
showType(t5, "t5");
showType(t6, "t6");
print(nl);
println("--- t6[1] = -2 --- : no copy on write");
t6[1] = -2;
isSameObject(t6, "t6", t5, "t5");
println(nil);
showType(t5, "t5");
showType(t6, "t6");
showNote("Source t5 modified!")
}
/*-----------------------------------------------------
display and analysis functions elided, see below
*/
</script>
<style>
body {
font-family:'Comic Sans MS', Tahoma;
padding:2em;
}
</style>
</head>
<body>
<div id="data"></div>
<script>
heading();
execute();
</script>
</body>
</html>
Javascript Data
-------------------------
Demo numbers
-------------------------
-- let t1 = 42 --
t1, number
size: 8, value: 42
-- let t2 = t1 --
t2 is same object as t1
-- let t2 = 3.1415927 -- : change of object
t2 is not same object as t1
t1, number
size: 8, value: 42
t2, number
size: 8, value: 3.1415927
-------------------------
Demo strings
-------------------------
-- let t3 = "a string" --
t3, string
size: 16, value: a string
-- let t4 = t3 --
t4 is same object as t3
t3, string
size: 16, value: a string
t4, string
size: 16, value: a string
-- t4 += " and another string" -- : copy on write
t4 is not same object as t3
t3, string
size: 16, value: a string
t4, string
size: 54, value: a string and another string
-------------------------
Demo arrays
-------------------------
--- t5 = [1, 2, 3] ---
t5, object
size: 24, value: 1,2,3
--- let t6 = t5 ---
t6 is same object as t5
t5, object
size: 24, value: 1,2,3
t6, object
size: 24, value: 1,2,3
--- t6[1] = -2 --- : no copy on write
t6 is same object as t5
t5, object
size: 24, value: 1,-2,3
t6, object
size: 24, value: 1,-2,3
-------------------------
Source t5 modified!
-------------------------
1.2 Display and Analysis Functions
Display & Analysis
/* analysis and display functions */
function print(str) {
document.write(str);
}
function println(str) {
document.write(str + "<br />");
}
function showNote(text) {
println("-------------------------");
println(text);
println("-------------------------");
}
/* Analysis functions */
function showType(t, nm) {
println(nm + ", " + typeof t);
print("size: " + sizeof(t) + ", value: " + t);
println(nil);
}
function isSameObject(o1, nm1, o2, nm2) {
if (o1 === o2) {
println(nm1 + " is same object as " + nm2)
}
else {
println(nm1 + " is not same object as " + nm2)
}
}
// https://gist.github.com/pgpbpadilla/10344038
function sizeof(object) {
var objectList = [],
stack = [ object ],
bytes = 0,
value,
i;
while (stack.length) {
value = stack.pop();
if (typeof value === 'boolean') {
bytes += 4;
} else if (typeof value === 'string') {
bytes += value.length * 2;
} else if (typeof value === 'number') {
bytes += 8;
} else if (typeof value === 'object'
&& objectList.indexOf(value) === -1) {
objectList.push(value);
for (i in value) {
if (value.hasOwnProperty(i)) {
stack.push(value[i]);
}
}
}
}
return bytes;
}
Display:
Analysis:
2.0 Build
local machine
C:\github\JimFawcett\Bits\Javascript\Js_Data
> start firefox C:\github\JimFawcett\Bits\Javascript\Js_Data\Js_Data.html
C:\github\JimFawcett\Bits\Javascript\Js_Data
>
github site
clone Bits repository and double click on Js_Data.html
3.0 VS Code View
4.0 References
Reference | Description |
---|---|
JavaScript Tutorial - w3schools | Interactive examples |
JavaScript Data Types - javascript.info | Summary with examples |
JavaScript Reference - MDN | Informal syntax reference |