1var age = 18; // number
2var name = "Jane"; // string
3var name = {first:"Jane", last:"Doe"}; // object
4var truth = false; // boolean
5var sheets = ["HTML","CSS","JS"]; // array
6var a; typeof a; // undefined
7var a = null; // value null
8
1/*
2The latest ECMAScript standard defines nine types:
3
4Six Data Types that are primitives, checked by typeof operator:
5undefined : typeof instance === "undefined"
6Boolean : typeof instance === "boolean"
7Number : typeof instance === "number"
8String : typeof instance === "string"
9BigInt : typeof instance === "bigint"
10Symbol : typeof instance === "symbol"
11Structural Types:
12Object : typeof instance === "object". Special non-data but Structural type for any constructed object instance also used as data structures: new Object, new Array, new Map, new Set, new WeakMap, new WeakSet, new Date and almost everything made with new keyword;
13Function : a non-data structure, though it also answers for typeof operator: typeof instance === "function". This is merely a special shorthand for Functions, though every Function constructor is derived from Object constructor.
14Structural Root Primitive:
15null : typeof instance === "object". Special primitive type having additional usage for its value: if object is not inherited, then null is shown;
16*/
1/*JavaScript data types*/
2//string
3var string = 'ASCII text';
4//int
5var integer = 123456789;
6//float
7var float = 123.456;
8//boolean, can be true or false
9var t = true;
10var f = false;
11//undefined
12var undef;//defaults to undefined
13var undef = undefined;//not common, use null
14//null
15var nul = null;
16//array
17var arr = ['Hello','my','name','is','Dr.Hippo',123,null];
18//object
19var person = {'name':'John Smith','age':27};
20//function
21var fun = function(){
22 return 42;
23}
1//String Data Type
2var strSingle = 'John'; //String with single quotes
3var strDouble = "Bob"; //String with double quotes
4
5//Number Data Type
6var num = 25; //Integer
7var flo = 80.5; //Floating-point number
8var exp = 4.25e+6; //Exponential notation, this equates to 4250000
9
10//Boolean Data Type
11var isReading = true; //Yes, I'm reading
12var isSleeping = false; //No, I'm not sleeping
13
14//Undefined Data Type
15var undef; //If a value is never assigned, any output will be 'undefined'
16
17//Null Data Type
18var noValue = null; //Null meaning that it is has no value, not the same as 0 or ""
19
20//Object Data Type
21var emptyObject = {};
22var person = {"name": "Clark", "surname": "Kent", "age": "36"}; //The quotes around the propety name can be omitted if the property name is a valid JS name
23var car = { //Same as person but easier to read
24 model: "BMW X3", //Example with quotes around property name ommitted
25 color: "white",
26 doors: 5
27}
28
29//Array Data Type
30var emptyArray = []; //An array can be of any data types (string, number, boolean, etc.)
31var array = ["One", "Two"] //String array, note the index of the first element is 0
32
33//Function Data Type
34var func = function() { //Calling the function: func();
35 alert("Code excuted"); //Outputs: Code executed
36}
37
38var funcVar = function(amount) { //Calling the function: funcVar(6);
39 alert("Code excuted " + amount + " times"); //Outputs: Code executed 6 times (if input was 6)
40}
41
42//Typeof Operator
43typeof variable; //Returns the data type of the variable
1dataTypes = {
2 Numbers = 1,2,3, 100, 3.14
3 Strings = 'Hello, World' "helloworld@gmail.com
4 Boolean = true / false
5 Null = 'Explicitly set a variable with no Value'
6 Undefined = 'For variable that have not yet been defined'
7 Object = 'Complex data structures - Arrays, Dates, Literals etc'
8 Symbol = 'Used with objects' // usually not needed
9}