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

[JavaScript Tutorial] JavaScript syntax

黄舟
黄舟Original
2016-12-24 14:30:45992browse

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

String literal can be written with double or single quotes:

"John Doe"

'John Doe'

Expression literal is used for calculations:

5 + 6

5 * 10

Array literal defines an array:

[40, 100, 1, 5, 25, 10]

Object (Object) literal defines an object:

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

Function (Function) literal defines a function:

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

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

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

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

x = 5
y = 6
z = (x + y) * 10

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 == != 6d267e5fab17ea8bc578f9e7e5e1570b 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;

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;

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 通过对象字面量赋值

数据类型的概念

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

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

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

16 + "Volvo"

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

"16Volvo"

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

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

JavaScript 函数

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

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

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

JavaScript 对大小写敏感。

JavaScript 对大小写是敏感的。

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

函数 getElementById 与 getElementbyID 是不同的。

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

JavaScript 字符集

JavaScript 使用 Unicode 字符集。

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

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

您知道吗?

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

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


Statement:
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