Home Web Front-end JS Tutorial [JavaScript Tutorial] JavaScript syntax

[JavaScript Tutorial] JavaScript syntax

Dec 24, 2016 pm 02:30 PM

JavaScript Syntax

JavaScript is a programming language. Grammar rules define the structure of a language.

JavaScript Syntax

JavaScript is a scripting language.

It is a lightweight, yet powerful programming language.

JavaScript Literal

In a programming language, a literal is a constant, as in 3.14.

Number literal can be an integer or decimal, or scientific notation (e).

3.14

1001

123e5
Copy after login

String literal can be written with double or single quotes:

"John Doe"

'John Doe'
Copy after login

Expression literal is used for calculations:

5 + 6

5 * 10
Copy after login

Array literal defines an array:

[40, 100, 1, 5, 25, 10]
Copy after login

Object (Object) literal defines an object:

{firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}
Copy after login

Function (Function) literal defines a function:

function myFunction(a, b) { return a * b;}
Copy after login

JavaScript variable

In programming languages, variables are used to store data values.

JavaScript uses the keyword var to define variables and the equal sign to assign values ​​to variables:

var x, length

x = 5

length = 6
Copy after login

Variables can be accessed through variable names. In imperative languages, variables are usually mutable. A literal is a constant value.


A variable is a name. A literal is a value.

JavaScript Operators

JavaScript uses arithmetic operators to calculate values:

(5 + 6) * 10
Copy after login

JavaScript uses assignment operators to assign values ​​to variables:

x = 5
y = 6
z = (x + y) * 10
Copy after login

The JavaScript language has many types of operators: /p>

Type

Instance

Description

Assignment, arithmetic and bitwise operators = + - * / Described in JS Operators

Conditional, comparison and logical operators == != < > In JS Comparison operators are described in

JavaScript Statements

In HTML, JavaScript statements issue commands to the browser.

Statements are separated by semicolons:

x = 5 + 6;
y = x * 10;
Copy after login

JavaScript keywords

JavaScript statements usually start with keywords. The var keyword tells the browser to create a new variable:

var x = 5 + 6;
var y = x * 10;
Copy after login

JavaScript Identifiers

Like any other programming language, JavaScript reserves some identifiers for its own use.

JavaScript also reserves some keywords that are not used in the current language version, but will be used in future JavaScript extensions.

JavaScript identifiers must start with a letter, underscore (_), or dollar sign ($).

Following characters can be letters, numbers, underscores or dollar signs (numbers are not allowed to appear as the first character so that JavaScript can easily distinguish identifiers from numbers).

The following are the most important reserved words in JavaScript (in alphabetical order):

abstract else instanceof super

boolean enum int switch

break export interface synchronized

byte extends let this this

case false long throw

catch final native throws

char finally new transient

class float null true

const for package try

continue function private typeof

debugger goto protected var

default if public void

delete implements return volatile

do import short while

double in static with

JavaScript 注释

不是所有的 JavaScript 语句都是"命令"。双斜杠 // 后的内容将会被浏览器忽略:

// 我不会执行

JavaScript 数据类型

JavaScript 有多种数据类型:数字,字符串,数组,对象等等:

var length = 16;                                  // Number 通过数字字面量赋值 
var points = x * 10;                              // Number 通过表达式字面量赋值
var lastName = "Johnson";                         // String 通过字符串字面量赋值
var cars = ["Saab", "Volvo", "BMW"];              // Array  通过数组字面量赋值
var person = {firstName:"John", lastName:"Doe"};  // Object 通过对象字面量赋值
Copy after login

数据类型的概念

编程语言中,数据类型是一个非常重要的内容。

为了可以操作变量,了解数据类型的概念非常重要。

如果没有使用数据类型,以下实例将无法执行:

16 + "Volvo"
Copy after login

16 加上 "Volvo" 是如何计算呢? 以上会产生一个错误还是输出以下结果呢?

"16Volvo"
Copy after login

你可以在浏览器尝试执行以上代码查看效果。

在接下来的章节中你将学到更多关于数据类型的知识。

JavaScript 函数

JavaScript 语句可以写在函数内,函数可以重复引用:

引用一个函数 = 调用函数(执行函数内的语句)。

function myFunction(a, b) {
    return a * b;                                // 返回 a 乘于 b 的结果
}
Copy after login

JavaScript 对大小写敏感。

JavaScript 对大小写是敏感的。

当编写 JavaScript 语句时,请留意是否关闭大小写切换键。

函数 getElementById 与 getElementbyID 是不同的。

同样,变量 myVariable 与 MyVariable 也是不同的。

JavaScript 字符集

JavaScript 使用 Unicode 字符集。

Unicode 覆盖了所有的字符,包含标点等字符。

如需进一步了解,请学习我们的 完整 Unicode 参考手册。

您知道吗?

JavaScript 中,常见的是驼峰法的命名规则,如 lastName (而不是lastname)。    

 以上就是【JavaScript教程】JavaScript 语法的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

TypeScript for Beginners, Part 2: Basic Data Types TypeScript for Beginners, Part 2: Basic Data Types Mar 19, 2025 am 09:10 AM

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript

See all articles