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 16// Data Types 17var name = "Alice"; 18let age = 25; 19const pi = 3.14; 20let isSame = true; 21let u = undefined; 22let n = null; 23const mySymbol = Symbol(); 24let val1 = 10n; 25let obj = { 26 mySymbol : "prop", 27}; 28let arr = [1, 2, 3]; 29//Function Type 30var sum = function(num1, num2) { 31 return num1 + num2; 32}; 33sum(1, 2); 34// Date Type 35let now = new Date(); 36 37console.log(name, age, pi, u, n, mySymbol, val1, obj, arr[1]); 38 39// if Statement 40if (age >= 18) { 41 console.log("Adult"); 42} else { 43 console.log("Minor"); 44} 45 46// Switch Statement 47let day = "Monday"; 48switch (day) { 49 case "Monday": 50 console.log("Start of the week"); 51 break; 52 case "Friday": 53 console.log("End of the week"); 54 break; 55 default: 56 console.log("Midweek"); 57} 58 59// For Statement 60for (let i = 0; i < 5; i++) { 61 console.log(i); 62} 63 64// While Statement 65let i = 0; 66while (i < 5) { 67 console.log(i); 68 i++; 69} 70 71do { 72 console.log(i); 73 i++; 74} while (i < 5); 75 76// Arrow Function 77const greet = (name) => { 78 console.log("Hello, " + name); 79}; 80greet("js"); 81 82// Object 83const person = { 84 name: "Alice", 85 age: 25, 86 greet() { 87 console.log("Hello, " + this.name); 88 } 89}; 90person.greet(); 91 92// Template string 93const temp = "Js"; 94const greeting = `Hello, ${temp}!`; 95 96// Destructuring assignment 97const student = { gender: "男", grade: "2"}; 98const { gender, grade } = person; 99 100// Default parameters 101function defaultGreet(name = "js") { 102 console.log("Hello, " + name); 103} 104defaultGreet(); 105 106// Extended operator 107const arr1 = [1, 2, 3]; 108const arr2 = [...arr1, 4, 5]; 109 110// Class 111class Person { 112 constructor(name, age) { 113 this.name = name; 114 this.age = age; 115 } 116 117 greet() { 118 console.log("Hello, " + this.name); 119 } 120} 121const alice = new Person("Alice", 25); 122alice.greet(); 123 124// Asynchronous function 125function fetchData() { 126 return new Promise((resolve, reject) => { 127 setTimeout(() => { 128 resolve('Data fetched successfully!'); 129 }, 1000); 130 }); 131} 132 133fetchData().then(data => { 134 console.log(data); 135}).catch(error => { 136 console.error('An error occurred:', error); 137}); 138 139async function fetchAndDisplayData() { 140 try { 141 const data = await fetchData(); 142 console.log(data); 143 } catch (error) { 144 console.error('An error occurred:', error); 145 } 146} 147fetchAndDisplayData();