Core Language Types

This document describes the Number, Boolean, Function and String types. Also see the Objects and Arrays document and the Class Library Reference.

References

These references contain extensive material about the JavaScript core language types.

Standard JavaScriptData Types

JavaScript (ECMA-262 3.X) supports the following data types.

Ejscript also supports the E4X (ECMA-357) XML extensions which add the following data types.

Ejscript System Library

Ejscript enhances the standard types with some extension methods and an extensive system library of classes and types. The Ejscript Web Framework provides additional classes to implement a Model / View / Controller framework for web applications.

Numbers

Standard JavaScript numbers are stored and manipulated as double floating point quantities, but for embedded applications, this is often less than ideal. Consequently, Ejscript allows the default numeric type to be selected at build time to be either: float, double, int or int64.

Numeric Literals

Numeric literals may be specified as octal values by prefixing with a 0, or hexadecimal by prefixing with 0x or 0X. If Number is configured to be based on a double floating point type, then numeric literals can use an exponential notation of the form: floating number followed by 'e' or 'E', followed by an optional sign '+' or '-', followed by an integer exponent. I.e.

[digits] [.digits] [E|e] [(+|-)digits]

For example:

var pi = 3.14e1
pi = 314.0e-2

Useful Constants

JavaScript defines some useful floating point constants. The global variable NaN is set to the "not a number" value. This value is the result when a math operation gives an undefined value or error. For example, divide by zero will result in the NaN value. The Infinity constant is set to the largest possible floating number, the result is equal to the Infinity value.

Booleans

A boolean value can be either true or false. Assigning either true or false to a variable or the result of a conditional expression will set the type of a variable to boolean. The following example will set x to true.

var y = 1
var x = (y == 1)

Ejscript defines two immutable global variables true and false with the appropriate boolean values.

Strings

Strings are immutable sequences of Unicode letters or escaped literals. String literals may be enclosed in either single or double quotes.

Ejscript strings are easy to work with due to the inbuilt ability to append strings via the + operator. For example:

var str = "Happy Birthday " + name
var greek = "&lambda expression"

JavaScripts supports the following escaped characters inside string literals:

Escape Sequence Description
\b Backspace (0x8)
\f Form feed (0xc)
\n New line (0xa)
\r Carriage return (0xd)
\t Horizontal tab (0x9)
\v Vertical tab (0xb)
\uNNNN Unicode character (0xNNNN)
\xNN Hexadecimal character (0xNN)
\NNN Octal character (ONNN)
\' Literal single quote
\" Literal double quote
\\ Literal backslash

Functions

A function is a script that can be executed by name with supplied arguments. Functions are declared via the function keyword followed by optional arguments and a function body contained within braces. For example:

function myPoint(x, y: Number): Number {
    return x + y
}

Functions can use a return statement to return an arbitrary value or object.

To invoke a function, the function name is used with the () operator following. The actual arguments are supplied within the "()" . The arguments to the function are evaluated left to right. For example:

var x = myPoint(1, y + 7)

Functions are values in JavaScript and may also be assigned to other variables or properties. They can also be passed as parameters into other functions. When a function is assigned to a property of an object it becomes a method for that object. When invoked via an object, the function's this property is set to the object.

Bound This

It is often useful to be able to pass functions as parameters without having to also pass an object on which to later invoke the method. Ejscript provides an enhancement such that if a function is read from an object without invoking it, Ejscript will capture the object reference and bind it to the function reference. When the function is later run, the this property of the function will be set to the object automatically.

    var run = obj.run
    after(10, run)

Objects and Arrays

See the Objects and Arrays document for further details.

Null and Undefined

JavaScript defines two special values: null and undefined. The null value means the property exists but either has not any assigned value or the null value has been assigned explicitly. The undefined value means the requested property does not exist.

Type Conversions

JavaScript supports a set of idioms that greatly eases the conversion of types from one type to another.

Adding a string to a number will cause the number to be converted to a string. Subtracting zero from a string will convert it to a number.

Initial Type Target Type Initial Value Final Value
Boolean Number true 1
Boolean Number false 0
Boolean String true "true"
Boolean String false "false"
Number Boolean 0, NaN false
Number Boolean all other values 0
Number String NaN "NaN"
Number String any number "string equivalent"
Object Boolean object true
Object Number object NaN
Object String object "[object Object]"
String Boolean "" false
String Boolean all other values true
String Number Number string numeric equivalent
String Number Non-numeric string NaN
undefined Boolean undefined false
undefined Number undefined NaN
undefined String undefined0 "undefined"
null Boolean null false
null Number null NaN
null String null "null"

Implicit Conversion Idioms

To explicitly force a type conversion, use the following idioms.

Initial Type Target Type Action Example
Number Boolean Negate twice !!num
Number String Add a string on the right num + ""
String Boolean Negate twice !!string
String Number Add a numeric on the right string - 0
Object String Invoke the toString method o.toString()
Object Boolean Negate twice !!obj

Implicit Conversions via Type Annotations

Ejscript adds support for optional type annotations on variable declarations and function parameters. If used, these type annotations can greatly simplify your code by automatically converting types.

If a variable is defined and contains a number and is then assigned a string value, JavaScript will normally assign the string, and the type of the variable will change from number to string. This can be the cause of subtle errors. However, if the number has an optional type annotation of "Number", then the compiler and VM will automatically convert the string to a number and ensure the variable only ever contains numbers.

var num: Number = 77
num = "1234"
print(typeof num)
/* Emits */
number

Similarly if a function can only take numeric arguments, the function often has to do costly type checking to ensure the args are of the required type.

function square(n) {
    if (typeof n != "number) {
        throw new Error("Bad Arg")
    }
    return n * n
}

But if optional type annotation is used on the args, then things are much simpler. The VM will cast the argument to a number before invoking the function. The function guarantees that the arguments will always be of the correct type.

function square(n: Number) {
    return n * n
}

For the exact semantics, please consult the ECMAScript-262 specification.

Explicit Conversion

While implicit conversion is very convenient, you sometimes need to explicitly control the type conversion.

You can use the explicit constructor functions: Boolean(), String() and Number() to force a conversion.

var num = Number("1234")

Ejscript also adds a cast operator for convenient control over casting.

Cast Operator

The cast operator is similar in syntax to instanceof, but allows you to cast an object explicitly to any other type.

var str = 1234 cast String

The cast operator is faster than using the conversion constructor functions as the compiler often knows if the type is compatible and can generate more optimal code.

Type Conversion Rules

  1. If the left operand is a string, the right hand operand will be converted to a string. If both operands are numeric, a numeric operation is done.
  2. If either operand is an object, it is converted to a string.
  3. If both operands are boolean and the operation is ==, !=, then proceed without a type conversion.
  4. If one operand is a string and the other is a numeric, try to convert the string to a number. Otherwise, try to convert the numeric to a string.

© Embedthis Software. All rights reserved.