JavaScript >> Define function, array, object, variable, class and property in JavaScript
Table of Contents
This tutorial will explain how to define function, array, object, variable, class and property in JavaScript.
JavaScript define function
Define using function keyword
function add(a, b) {
return a + b;
}
Use arrow syntax to define a function
let add = (a, b) => {
return a + b;
}
JavaScript define array
Define an empty array and add/remove elements
let arr = [];
arr.push(1);
arr.push(2);
arr.push(3);
console.log(arr);
arr.pop(); // remove the last element
console.log(arr);
[1, 2, 3]
[1, 2]
Define an array in ES6.
let array = [1, 2, 3];
console.log(array)
[1, 2, 3]
Use Array object to define an array.
let a = new Array(10);
a[0] = "abc"
a[9] = "aaa"
console.log(a)
(10) ['abc', empty × 8, 'aaa']
JavaScript define object
Define object in Literal Syntax.
let obj = {
name: "Kevin",
age: 30,
sayHello: function() {
console.log("Hello, " + this.name);
}
};
obj.sayHello();
Hello, Kevin
Define object using constructor.
function Person(name, age) {
this.name = name;
this.age = age;
}
let person = new Person("Kevin", 30);
console.log(person.name);
Kevin
JavaScript define varaible
Define variables using var keyword in ES5
var age = 20;
var name = "Kevin";
console.log(name + "'s age is " + age);
Kevin's age is 20
Define variables using let keyword in ES6
ES6 introduced the let keyword to define variables.
let age = 20;
let name = "Kevin";
console.log(name + "'s age is " + age);
Define local and global variables
var global = "global";
function test() {
var local = "local";
console.log("Reference global variable in local scope: " + global);
console.log("Reference local variable in local scope: " + local);
}
test();
console.log("Reference global variable in global scope: " + global);
console.log("Reference local variable in global scope: " + local); // Error will occur
Reference global variable in local scope: global
Reference local variable in local scope: local
Reference global variable in global scope: global
Uncaught ReferenceError: local is not defined
Define constant using const keyword in ES6
The constant can’t be changed after it is defined.
const PI = 3.14;
console.log(PI);
3.14
If we try to change it, we will get an error.
PI = 2.0;
TypeError: Assignment to constant variable.
Define mutiple variables in one line.
var name = "Kevin", age = 30;
let a = 20, b = "test";
In ES6, we can define multiple variables in one line as follows.
let [a,b,c,d] = [0,1,2,3];
JavaScript define Class
class Student {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log("Hello, " + this.name);
}
}
let s = new Student("Kevin", 16);
s.sayHello();
console.log(s.name + "'s age is " + s.age);
Hello, Kevin
Kevin's age is 16
JavaScript define property
const student = {
name: "Kevin",
age: 16,
sayHello() {
console.log("Hello, " + this.name);
}
};
Object.defineProperty(student, "score", {
writable: true,
value: 0
});
student.score = 88;
console.log(student.name + "'s score is " + student.score);
Kevin's score is 88