Home » Technology » JavaScript Naming Rules, Data Types, and Objects

JavaScript Naming Rules, Data Types, and Objects

Naming rules

JavaScript naming rules are quite important, it has some specific conventions and restrictions. For example, variables and functions are often named using lower camel case (firstName). JavaScript is then case sensitive, so lastName and lastname are different variables. Comments can be single-line comments // or multi-line comments /* */.

data type

JavaScript’s basic data types, including boolean (true/false), empty (null), undefined (undefined), value (number), string (string) and object (Object). Objects are composite data types. For example, Array is a subtype of Object.There are also some special values ​​like empty array[]and NaN (Not a Number).

Objects and Arrays

Objects and arrays are commonly used data structures in JavaScript. Objects are defined using and can have properties and methods. For example: This object has three properties: title, author and pages, and a method read.

var book =
title: “My first book”,
author: “Xiao Ming”,
pages: 100,
read: function()
return “You are reading ” + this.title + ” written by ” + this.author + “.”;

;

console.log(book.read());

Arrays use square brackets[]To define, multiple values ​​can be stored. For example: This array has three elements which can be accessed by index 0, 1 and 2.

var fruits = [“蘋果”, “香蕉”, “橙子”];

console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.