Basic Reference Types

Introduction

This section I will be covering Objects in greater detail and the basic data types and finally working with primitives and primitive wrappers

Date Type

Javascript provides a date type object and is based on the Java version, it stores dates from Jan 1 1970 using milliseconds pass this date, if you don't pass any arguments it will store the current date and time. There are many functions that you can use which I will detail below

current date/time
let now = new Date();
specific date/time
let someDate = new Date(2019, 0, 1);
let someDate = new Date("May 23, 2019");
        
let someDate = new Date(Date.UTC(2020, 4, 5, 17, 55, 55));
get Specific elements of a date
let someDate = new Date("May 23, 2019");

console.log(someDate.toString());
console.log(someDate.getFullYear())
console.log(someDate.getMonth())
console.log(someDate.getDate())
console.log(someDate.getDay())

Note: there are many methods to extract specific date, month, date, day, hours, mintues, seconds, etc

Regular Expressions

Regular expressions are used by many programming languages, it made up of 3 parts, th string to manipulate, the pattern and any flags

Regexp flags
  • g - means global, the regexp is applied to the string as a whole
  • i - means ignore case
  • m - means multi-line, the regexp will look until the end of a line
  • y - means sticky mode, meaning the pattern will only look at the string contents beginning at lastIndex
  • u - means Unicode is enabled
regexp instance properties
let pattern1 = /\[bc\]at/i;
        
console.log(pattern1.global);                     // false
console.log(pattern1.ignoreCase);                 // true
console.log(pattern1.multiline);                  // false
console.log(pattern1.lastIndex);                  // 0
console.log(pattern1.source);                     // "\[bc\]at"
console.log(pattern1.flags);                      // "i"
RegExp instance methods
let text = "mom and dad and baby";
let pattern = /mom( and dad( and baby)?)?/gi;
let matches = pattern.exec(text);

console.log(matches.index);                 // 0
console.log(matches.input);                 // "mom and dad and baby"
console.log(matches[0]);                    // "mom and dad and baby"
console.log(matches[1]);                    // " and dad and baby"
console.log(matches[2]);                    // " and baby"
RegExp constructor properties
let text = "this has been a short summer";
let pattern = /(.)hort/g;

if (pattern.test(text)) {
    console.log(RegExp.input); // this has been a short summer
    console.log(RegExp.leftContext); // this has been a
    console.log(RegExp.rightContext); // summer
    console.log(RegExp.lastMatch); // short
    console.log(RegExp.lastParen); // s
}
regular expression examples
let pattern1 = /at/g;                // Match all instances of "at" in a string
let pattern2 = /[bc]at/i;            // Match the first instance of "bat" or "cat", regardless of case
let pattern3 = /.at/gi;              // Match all three-character combinations ending with "at", regardless of case

I have also covered Regular Expressions in my Java and Python sections.

Primitive Wrapper Types

There are three special wrapper types in Javascript, Boolean, Number and String types, these Object types have methods that you can use to manipulate the data in the Object. There are many methods that you can use i list a few examples below

Boolean Type
let trueObject = new Boolean(true);
let falseObject = new Boolean(false);

let result = falseObject && true;                          // true

console.log(typeof falseObject);                           // object
console.log(typeof falseValue);                            // boolean
console.log(falseObject instanceof Boolean);               // true
console.log(falseValue instanceof Boolean);                // false

Number Type
let numberObject = new Number(10);
console.log(num.toString());                 // "10"

console.log(num.toString(2));                // "1010"
console.log(num.toString(8));                // "12"
console.log(num.toString(10));               // "10"
console.log(num.toString(16));               // "a"
console.log(num.toFixed(2));                 // "10.00"
console.log(num.toPrecision(3));             // "99.0"
String Type
let stringValue = new String("hello world");
console.log(stringValue.length);                     // "11"
console.log(stringValue.charAt(2));                  // "c"

let result = stringValue.concat("world");
let result = stringValue.concat("world", "!");

let stringValue = "hello world";
console.log(stringValue.slice(3));                   // "lo world"
console.log(stringValue.substring(3));               // "lo world"
console.log(stringValue.substr(3));                  // "lo world"
console.log(stringValue.slice(3, 7));                // "lo w"
console.log(stringValue.substring(3,7));             // "lo w"
console.log(stringValue.substr(3, 7));               // "lo worl"
console.log(stringValue.slice(-3));                  // "rld"
console.log(stringValue.substring(-3));              // "hello world"

console.log(stringValue.startsWith("foo"));          // true
console.log(stringValue.startsWith("bar"));          // false
console.log(stringValue.endsWith("baz"));            // true
console.log(stringValue.endsWith("bar"));            // false
console.log(stringValue.includes("bar"));            // true
console.log(stringValue.includes("qux"));            // false

console.log(stringValue.toLocaleUpperCase());        // "HELLO WORLD"
console.log(stringValue.toUpperCase());              // "HELLO WORLD"

Singleton Objects

There are two singleton built-in Objects Global and Math, The Global object is not explicity accessible, its a catch all object for properties and methods that don't have an object, this is were all global variables reside, it also has many functions like isNaN(), parseInt(), etc, the Global object also has many properties such as Object, Array Function, etc all of which are constructors for creating Objects. There are many more here are a few wexamples which I want to make you aware of

Global Object methods
let uri = "http:// www.wrox.com/illegal value.js#start";
console.log(encodeURI(uri));
        
et uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start";
console.log(decodeURI(uri));

let msg = "hello world";
eval("console.log(msg)");                     // "hello world"

Note: The eval() function evaluates or executes an argument.

The Math object provides mathematical formulas, information and computation. The object has a number of properties like PI, SQRT2, etc and many methods like max(), min(), ceil(), floor(), random(), etc.

MATH object properties and functions
let pi = Math.PI

let max = Math.max(3, 54, 32, 16);
let min = Math.min(3, 54, 32, 16);
          
console.log(Math.ceil(25.9));             // 26
console.log(Math.ceil(25.5));             // 26
console.log(Math.ceil(25.1));             // 26
          
console.log(Math.round(25.9));            // 26
console.log(Math.round(25.5));            // 26
console.log(Math.round(25.1));            // 25
          
console.log(Math.floor(25.9));            // 25
console.log(Math.floor(25.5));            // 25
console.log(Math.floor(25.1));            // 25

number = Math.floor(Math.random() * 10 + 1)    // 10 = choices and 1 = lowver value