Basic Objects

Loading...

Overview

See MDN 'Fundamental objects'

Shared JavaScript

This section contains code that can be shared by all experiments on this page. For example, the ObjectsLogHelper provides convenience methods for logging results specific to the experiments on this page.

Fundamental Objects

Object

// Object literal { "name1": value1, ... } // Object constructor new Object( [ value ] )

Object Literal

Function

// Function constructor new Function ([arg1[, arg2[, ...argN]],] functionBody)

Boolean

// Boolean constructor new Boolean(value) // Preferred form for conversion Boolean(value)

Caution - Non-null objects are implicitly true (even if it's a boolean set to false), whereas the null object is always false.

Basic Evaluation

Non-Obvious Evaluation

Error

new Error([message[, fileName[,lineNumber]]])

Numbers and Dates

Number

A double-precision, 64-bit binary format, IEEE 754 value.

// Number constructor new Number(value); // Preferred form for conversion Number(value);

Properties

Conversions

Date

Construction

new Date(); new Date(integerValue); new Date(dateString); // recognizable by Date.parse() new Date(year, month, day, hour, minute, second, millisecond);

For max and min dates, see Minimum and maximum date.

Accessors

UTC

Conversions

Parse

Interprets the given string as a date and time and returns it as a UTC time value. At first, it attempts to parse the format of the String according to the Date Time String Format rules. Failing that, it may fall back to any implementation-specific date formats. Invalid Strings will cause Date.parse to return NaN.

Date.parse(string) // YYYY-MM-DDTHH:mm:ss.sssZ

Misc

Math

Not a function object

String

Note: JavaScript distinguishes between String objects and primitive string values. JavaScript automatically converts primitives to String objects.

"String Literal" String(str) new String(str)

RegExp

See Regular Expressions in the JavaScript Guide.

/pattern/flags; new RegExp(pattern [, flags]);

Test

regexObj.test(str)

The following experiments test for simple patterns within a series of test strings.

Digits separated by periods (e.g., 'aa.1.22.333.4444.bb')

This experiment tests for the presence of at least one occurrence of "digit(s) dot digit(s)", while ignoring all leading and trailing non-digit characters (e.g., '+', '-', 'abc').

Digits separated by one period (e.g., 'aa.1.22.bb')

This experiment tests for the existence of exactly one occurrence of "digit(s) dot digit(s)", starting at the beginning of the string and ignoring all leading and trailing non-digit characters (e.g., '+', '-', 'abc').

The string "aa 11 bb 22.33" will fail because the pattern requires a period after the first occurrence of digit(s), as stipulated by the addition of the "^" character at the beginning of the pattern.

The string "12.34.56" will fail because the pattern requires that only one period exist between the start and the end of the string, as stipulated by the "^" and "$" characters at the begin and end of the pattern.

Only digits separated by one period (e.g., '123.456')

This experiment tests that the entire string conforms to the pattern: "digit(s) dot digit(s)".

Replace

str.replace(regexp|substr, newSubStr|function[, flags]);

The following experiments replace simple matched patterns within a series of test strings. To replace multiple occurrences, see the Global Option Experiment

Digits separated by periods (e.g., 'aa.1.22.333.4444.bb')

This experiment replaces only the first occurrence of "digit(s) dot digit(s)", ignoring all non-digit characters (e.g., '+', '-', 'abc') surrounding the pattern.

Extract data from String

Options

Option: multiLine - OFF

This experiment illustrates what happens when the "m" option is false, but the pattern requires a match to start relative to the beginning of a line. If the pattern is matched on the second line, the test will fail because the regex stops searching after the first line.

Option: multiLine - ON

This experiment illustrates what happens when the "m" option is true. The same pattern and strings are used from the previous experiment plus additional multi-line test strings have been added.

In order to find matches, relative to the beginning of a line for a multi-line test string, use the 'm' option.

Option: ignoreCase - ON

This experiment illustrates what happens when the "i" option is true. The same pattern from the previous experiment is used, but the alpha-characters are now multi-cased.

In order to use case-insensitive matches, use the 'i' option.

Option: global - ON

This experiment illustrates what happens when the "g" option is true. The expressions and test strings are from the Replace experiments. In those experiments, only the first occurrence of the matched pattern was replaced. In this experiment, every match is replaced.

In order to replace multiple occurrences in a given string, use the 'g' option.