1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16let array = ['1', '2', '3']; 17let map = new Map(); 18map.set('a', 1); 19map.set('b', 2); 20 21function func() { 22 23} 24 25let objs = [array, map, func]; 26 27// integer or float constant 28let num1 = 42; // int 29let num2 = 3.14; // float 30let bigInt = BigInt(9007199254740991); 31let num3 = NaN; // nan number 32let num4 = Infinity; // Positive infinity 33let num5 = -Infinity; // negative infinity 34 35// string 36let str1 = 'Hello'; 37let str2 = 'World'; 38let str3 = `This is a template string. ${str1} ${str2}!`; 39 40// bool 41let bool1 = true; 42let bool2 = false; 43 44// null type 45let nullVar = null; 46 47// undefined type 48let undefinedVar; 49 50// array type 51let arr0 = new Int8Array(); 52let arr1 = new Uint8Array(); 53let arr2 = new Int16Array(); 54let arr3 = new Uint16Array(); 55let arr4 = new Int32Array(); 56let arr5 = new Uint32Array(); 57let arr6 = new BigInt64Array(); 58let arr7 = new BigUint64Array(); 59let arr8 = new Float32Array(); 60let arr9 = new Float64Array(); 61let arr10 = new Uint8ClampedArray(); 62let buffer = new ArrayBuffer(8); 63let sharedBuf = new SharedArrayBuffer(1024); 64 65let list = [1, 2, 3, 4, 5]; 66for (let item of list) { 67 print(item); 68} 69 70// funtion 71function greet() { 72 print('Hello, this is a function!'); 73} 74 75// data type 76let date = new Date(); 77 78// lambda 79let regex = /hello/i; // Case insensitive matching "hello" 80 81// object type 82let obj = { 83 name: 'Alice', 84 age: 25, 85 greet: function() { 86 print('Hello, my name is ' + this.name); 87 } 88}; 89 90// class 91class Person { 92 constructor(name, age) { 93 this.name = name; 94 this.age = age; 95 } 96 97 greet() { 98 print(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); 99 } 100} 101 102globalThis.person = new Person('Alice', 25); 103 104// Exception 105function divide(a, b) { 106 if (b === 0) { 107 throw new Error('The divisor cannot be 0'); 108 } 109 return a / b; 110} 111 112try { 113 let result = divide(10, 0); 114} catch (error) { 115 print('\nthrow statement\n'); 116} 117 118// Promise 119let promise = new Promise((resolve, reject) => { 120 // Simulated asynchronous operation 121 setTimeout(() => { 122 let success = false; // Simulated operation failed 123 if (success) { 124 resolve('operation success'); 125 } else { 126 reject(new Error('operation failed')); 127 } 128 }, 1000); 129}); 130 131promise.then((result) => { 132 print(result); 133}).catch((error) => { 134 print('Promise handle\n'); 135}); 136 137// Asynchronous type 138async function fetchDataWithAsyncAwait() { 139 return 'async/await get the data'; 140} 141 142async function fetchAndLogData() { 143 try { 144 const data = await fetchDataWithAsyncAwait(); 145 } catch (err) { 146 print('ERROR:', err); 147 } 148} 149 150fetchAndLogData(); 151