Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DATATYPES:
- *
- * 0. Javascript can accomodate different types of data in primitive and complex forms. When declaring
- * a variable, it is not necessary to declare a datatype. The type of variable will be automatically
- * determined during program execution.
- *
- * 1. Primitive types utilize physical memory in that, each value is stored within the 8 bytes allocated
- * for each variable.
- *
- * 2. Complex types are not stored in memory directly. Since they do not have a fixed size, only the
- * reference to the value is stored in each variable.
- */
- /*
- * PRIMITIVE DATATYPES
- */
- // Number
- var a = 25.4; // can include decimals
- var b = 123e-5; // scientific notation used for larger numbers
- // String
- var c = "Sample string of characters!"; // a sequence of characters enclosed in quotations
- // Boolean
- var d = true; // can be one of two states: true or false
- var e = false;
- // Undefined
- var f; // When a variable is declared without a value, the default value is 'undefined'.
- console.log(f);
- // Null
- var g = null; // Null means "nothing". The Value is null, but the type is considered an object.
- // NaN
- var h = NaN; // "Not a number" is still considered a 'number' type despite its name. NaN is returned
- console.log("pete"/5); // when something cannot be converted in to a number type.
- // Infinity
- // As in mathematics, any division by zero will result in an "infinity" type.
- // If the dividend is a negative number, the operation will result in "-infinity"
- var i = -5/0;
- console.log(i);
- /*
- * COMPLEX DATATYPES
- */
- // Array
- // A sequence of values enclosed in square brackets and separated by commas. Each value has an index
- // which is '0' for the first value in the array.
- //
- var j = [4, 6, 30, 9, 80]; // the variable 'k' stores the reference to the memory location for the array
- var k = ["Frank", "Jerry", "Kim"]; // each value can be accessed using l[index]
- console.log(j[3]); // prints the 4TH value in array 'k'.
- // Object
- // Similar to array, but instead of a sequence of single values, an object is a sequence of
- // 'name':'value' pairs. Instead of an index, the values are accessd by their corresponding 'name'.
- //
- var l = {
- id: 42,
- color: "gray",
- type: "laminated",
- };
- console.log(l.color); // prints "gray"
- // Function
- // A section of code that can be re-used and called upon if needed. Using functions is a 2 step
- // process: Declaration and Execution. Values can be 'passed' to the function using parameters in the
- // funciton declaration. We can call a funciton many times without having to write the same code
- // over and over
- //
- function test(number) { // Declaration
- console.log("Testing " + number);
- }
- test(5); // Execution - Prints "Testing 5"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement