SJS supports the following data types:
String: string
Boolean: A Boolean value (true or false).
Number: A numeric value.
Object: object
Function: function
Array: array
Date: Date
Regexp (regular expression)
Determine the data type
SJS provides two ways to determine the data type: constructor and typeof.
constructor
const number = 10;
console.log(number.constructor); // "Number"
const string = "str";
console.log(string.constructor); // "String"
const boolean = true;
console.log(boolean.constructor); // "Boolean"
const object = {};
console.log(object.constructor); // "Object"
const func = function(){};
console.log(func.constructor); // "Function"
const array = [];
console.log(array.constructor); // "Array"
const date = getDate();
console.log(date.constructor); // "Date"
const regexp = getRegExp();
console.log(regexp.constructor); // "RegExp"typeof
const num = 100;
const bool = false;
const obj = {};
const func = function(){};
const array = [];
const date = getDate();
const regexp = getRegExp();
console.log(typeof num); // 'number'
console.log(typeof bool); // 'boolean'
console.log(typeof obj); // 'object'
console.log(typeof func); // 'function'
console.log(typeof array); // 'object'
console.log(typeof date); // 'object'
console.log(typeof regexp); // 'object'
console.log(typeof undefined); // 'undefined'
console.log(typeof null); // 'object'String
Syntax
'hello alipay';
"hello taobao";ES6 syntax
// String template
const a = 'hello';
const str = `${a} alipay`;Attribute
constructor: Returns"String".length
For more information about attributes other than constructor, see the ES6 standard.
Method
toString
valueOf
charAt
charCodeAt
concat
indexOf
lastIndexOf
localeCompare
match
replace
search
slice
split
substring
toLowerCase
toLocaleLowerCase
toUpperCase
toLocaleUpperCase
trim
For usage details, see the ES6 standard.
Number
Syntax
const num = 10;
const PI = 3.141592653589793;Attribute
constructor: Returns "Number".
Method
toString
toLocaleString
valueOf
toFixed
toExponential
toPrecision
For usage details, see the ES6 standard.
Boolean
A Boolean value can only be one of two specific values: true or false.
Syntax
const a = true;Attribute
constructor: Returns"Boolean".
Method
toString
valueOf
For usage details, see the ES6 standard.
Object
Syntax
var o = {}; // Create a new empty object
// Create a new non-empty object
o = {
'str': "str", // The key of an object can be a string
constVar: 2, // The key of an object can also be an identifier that follows variable naming rules
val: {}, // The value of an object can be of any type
};
// Read an object attribute
console.log(1 === o['string']);
console.log(2 === o.constVar);
// Write to an object attribute
o['string']++;
o['string'] += 10;
o.constVar++;
o.constVar += 10;
// Read an object attribute
console.log(12 === o['string']);
console.log(13 === o.constVar);ES6 syntax:
// Supported
let a = 2;
o = {
a, // Object attribute
b() {}, // Object method
};
const { a, b, c: d, e = 'default'} = {a: 1, b: 2, c: 3}; // Object destructuring assignment & default
const {a, ...other} = {a: 1, b: 2, c: 3}; // Object destructuring assignment
const f = {...others}; // Object destructuringAttribute
constructor: Returns "Object".
console.log("Object" === {a:2,b:"5"}.constructor);Method
toString: Returns "[object Object]".
Function
Syntax
// Method 1: Function declaration
function a (x) {
return x;
}
// Method 2: Function expression
var b = function (x) {
return x;
};
// Method 3: Arrow function
const double = x => x * 2;
function f(x = 2){} // Default function parameter
function g({name: n = 'xiaoming', ...other} = {}) {} // Destructuring assignment for function parameters
function h([a, b] = []) {} // Destructuring assignment for function parameters
// Anonymous function, closure
var c = function (x) {
return function () { return x;}
};
var d = c(25);
console.log(25 === d());The arguments keyword is available within a function.
var a = function(){
console.log(2 === arguments.length);
console.log(1 === arguments[0]);
console.log(2 === arguments[1]);
};
a(1,2);Output:
true
true
trueAttribute
constructor: Returns"Function".length: Returns the number of formal parameters of the function.
Method
toString: Returns "[function Function]".
Example
var f = function (a,b) { }
console.log("Function" === f.constructor);
console.log("[function Function]" === f.toString());
console.log(2 === f.length);Output:
true
true
trueArray
Syntax
var a = []; // Empty array
a = [5,"5",{},function(){}]; // Non-empty array. Array elements can be of any type.
const [b, , c, d = 5] = [1,2,3]; // Array destructuring assignment & default value
const [e, ...other] = [1,2,3]; // Array destructuring assignment
const f = [...other]; // Array destructuringAttribute
constructor: Returns"Array".length
For more information about attributes other than constructor, see the ES6 standard.
Method
toString
concat
join
pop
push
reverse
shift
slice
sort
splice
unshift
indexOf
lastIndexOf
every
some
forEach
map
filter
reduce
reduceRight
For usage details, see the ES6 standard.
Date
Syntax
You can use the getDate function to create a Date object. When called without arguments, the function returns an object that represents the current time.
getDate()
getDate(milliseconds)
getDate(datestring)
getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])Parameters
milliseconds: The number of milliseconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC) on January 1, 1970.datestring: A date string in the format "Month Day, Year Hours:Minutes:Seconds".
Attribute
constructor: Returns "Date".
Method
toString
toDateString
toTimeString
toLocaleString
toLocaleDateString
toLocaleTimeString
valueOf
getTime
getFullYear
getUTCFullYear
getMonth
getUTCMonth
getDate
getUTCDate
getDay
getUTCDay
getHours
getUTCHours
getMinutes
getUTCMinutes
getSeconds
getUTCSeconds
getMilliseconds
getUTCMilliseconds
getTimezoneOffset
setTime
setMilliseconds
setUTCMilliseconds
setSeconds
setUTCSeconds
setMinutes
setUTCMinutes
setHours
setUTCHours
setDate
setUTCDate
setMonth
setUTCMonth
setFullYear
setUTCFullYear
toUTCString
toISOString
toJSON
For usage details, see the ES6 standard.
Example
let date = getDate(); // Returns an object for the current time
date = getDate(1500000000000);
// Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)
date = getDate('2016-6-29');
// Fri June 29 2016 00:00:00 GMT+0800 (China Standard Time)
date = getDate(2017, 6, 14, 10, 40, 0, 0);
// Fri Jul 14 2017 10:40:00 GMT+0800 (China Standard Time)Regexp
Syntax
You can use the getRegExp function to create a Regexp object.
getRegExp(pattern[, flags])Parameters
pattern: The text of the regular expression.
flags: Modifiers. The value is a string that can contain any of the following characters:
g: globali: ignoreCasem: multiline
Attribute
constructor: Returns
"RegExp".global
ignoreCase
lastIndex
multiline
source
For more information about attributes other than constructor, see the ES6 standard.
Method
exec
test
toString
For usage details, see the ES6 standard.
Example
var reg = getRegExp("name", "img");
console.log("name" === reg.source);
console.log(true === reg.global);
console.log(true === reg.ignoreCase);
console.log(true === reg.multiline);