Java Script Syntax

JavaScript  syntax


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

JavaScript syntax

JavaScript is a scripting language.

It is a lightweight, yet powerful programming language.


JavaScript literals

In programming languages, it is generally referred to as literal constant value, such as 3.14.

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

3.14

1001

123e5

String (String) literal  can use single or double quotes:

"John Doe"

'John Doe'

It can be used to calculate the expression literals

5 + 6

5 * 10

Array (the Array) literal  definition of an array:

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

Object (Object) literal  definition of an object:

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

Function (Function) literal  definition of a function:

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

 

JavaScript Variables

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

JavaScript uses the keyword  var  to define a variable, use an equal sign to assign a variable:

var x, length

x = 5

length = 6

JavaScript operator

JavaScript uses  arithmetic operators  calculated value:

(5 + 6) * 10

JavaScript uses the assignment operator to assign values to variables:

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

There are many types of JavaScript language operators:

?Types of Examples description
Assignment, arithmetic and bitwise operators =  +  -  *  / JS operator described in
Conditions and logical operators Comparison ==  != <  >  It is described in the JS comparison operators

 

JavaScript statements

In HTML, JavaScript statement is a command to your browser.

Statements are separated by semicolons:

x = 5 + 6;
y = x * 10;

JavaScript Keyword

JavaScript keyword is used to identify the operation to be performed.

And any other programming languages, JavaScript retain some keywords for their own use.

var  keyword tells the browser to create a new variable:

where x = 5 + 6 ;
where y = x * 10 ;

JavaScript comment

Not all JavaScript statements are "command." Double slash  //  contents of the browser will be ignored:

// I will not be executed

JavaScript data types

JavaScript has multiple data types: numeric, strings, arrays, and the like objects:

var length = 16 ;                                   // Number The digital literal assignment 
var Points * X = 10 ;                               // Number The literal assigned by the expression 
var lastName = " Johnson " ;                          // String literal string assigned by 
var cars = [ " saab " , " Volvo " , " the BMW " ];               // the array array literal assigned by 
var Person = {firstName: " John ", LastName: " Doe " };   // Object literal assigned by the object

 


The concept of data types

Programming languages, data types is a very important element.

In order to be operational variables, data types to understand the concept is very important.

If you do not use the data type, the following examples will not be executed:

16 + "Volvo"

16 plus "Volvo" is how to calculate it? Above will generate an error or output the following result?

"16Volvo"

You can try to execute the code above to see the effect in the browser.

In the next chapter you will learn more about data types.

JavaScript function

JavaScript statements can be written in a function, the function can be repeated quote:

Reference to a function  = calling the function (statements within the function is executed).

myFunction function (a, b) {
        return a * b;                                 // return a result of multiplying b 
}

JavaScript letter case

JavaScript is case-sensitive.

When you write JavaScript statements, please pay attention to whether to close the case toggle key.

Function  getElementById  and  getElementbyID  is different.

Similarly, the variable  myVariable  and  MyVariable  is different.

JavaScript Character Set

JavaScript use Unicode character set.

It covers all Unicode characters including punctuation and other characters.

For more information, please study our  full Unicode reference manual .

 

JavaScript  statement


JavaScript statement is a command to your browser. Statement is used to tell the browser what to do.


JavaScript statements

JavaScript statement is a command sent to the browser.

The role of these commands is to tell the browser to do.

The following JavaScript statements to id = "demo" HTML elements output the text "Hello Dolly":

document.getElementById("demo").innerHTML = "你好 Dolly";

Semicolon;

JavaScript semicolon is used to separate statements.

We usually add a semicolon at the end of each executable statement.

Another use a semicolon is to write multiple statements in a row.

a = 5;
b = 6;
c = a + b;

The above examples can also write:

a = 5; b = 6; c = a + b;

JavaScript code

JavaScript code is a sequence of JavaScript statements.

Browser in order to execute each statement in accordance with the written order.

This example of the output to a page title and two paragraphs:

document.getElementById ( " Demo " ) .innerHTML = " Hello Dolly " ; 
document.getElementById ( " myDiv " ) .innerHTML = " how are you recently? " ;

JavaScript code block

JavaScript can be combined in batches.

Code block starts with a left curly brace and ends with the closing brace.

Action code block is a sequence of statements executed collectively.

This example of the output to a page title and two paragraphs:

myFunction function () 
{ 
    document.getElementById ( " Demo " ) .innerHTML = " Hello Dolly " ; 
    document.getElementById ( " myDiv " ) .innerHTML = " how are you recently? " ; 
}

Blank

JavaScript ignores extra spaces. You can add space to the script to make it more readable. The following two lines of code are equivalent:

var person="Hege";
var person = "Hege";

对代码行进行折行

您可以在文本字符串中使用反斜杠对代码行进行换行。下面的例子会正确地显示:

document.write("你好 \
世界!");

JavaScript 注释


JavaScript 注释可用于提高代码的可读性。


JavaScript 注释

JavaScript 不会执行注释。

我们可以添加注释来对 JavaScript 进行解释,或者提高代码的可读性。

单行注释以 // 开头。

本例用单行注释来解释代码:

// 输出标题:
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
// 输出段落:
document.getElementById("myP").innerHTML="这是我的第一个段落。";

JavaScript 多行注释

多行注释以 /* 开始,以 */ 结尾。

下面的例子使用多行注释来解释代码:

/*
下面的这些代码会输出
一个标题和一个段落
并将代表主页的开始
*/
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
document.getElementById("myP").innerHTML="这是我的第一个段落。";

使用注释来阻止执行

在下面的例子中,注释用于阻止其中一条代码行的执行(可用于调试):

// document.getElementById("myH1").innerHTML="欢迎来到我的主页";
document.getElementById("myP").innerHTML="这是我的第一个段落。";

 

在下面的例子中,注释用于阻止代码块的执行(可用于调试):

/*
document.getElementById("myH1").innerHTML="欢迎来到我的主页";
document.getElementById("myP").innerHTML="这是我的第一个段落。";
*/

在行末使用注释

在下面的例子中,我们把注释放到代码行的结尾处:

var x=5;    // 声明 x 并把 5 赋值给它
var y=x+2;  // 声明 y 并把 x+2 赋值给它

JavaScript 变量


变量是用于存储信息的"容器"。

 


就像代数那样

x=5
y=6
z=x+y

在代数中,我们使用字母(比如 x)来保存值(比如 5)。

通过上面的表达式 z=x+y,我们能够计算出 z 的值为 11。

在 JavaScript 中,这些字母被称为变量。

JavaScript 变量

与代数一样,JavaScript 变量可用于存放值(比如 x=5)和表达式(比如 z=x+y)。

变量可以使用短名称(比如 x 和 y),也可以使用描述性更好的名称(比如 age, sum, totalvolume)。

  • 变量必须以字母开头
  • 变量也能以 $ 和 _ 符号开头(不过我们不推荐这么做)
  • 变量名称对大小写敏感(y 和 Y 是不同的变量)

JavaScript 数据类型

JavaScript 变量还能保存其他数据类型,比如文本值 (name="Bill Gates")。

在 JavaScript 中,类似 "Bill Gates" 这样一条文本被称为字符串。

JavaScript 变量有很多种类型,但是现在,我们只关注数字和字符串。

当您向变量分配文本值时,应该用双引号或单引号包围这个值。

当您向变量赋的值是数值时,不要使用引号。如果您用引号包围数值,该值会被作为文本来处理。

声明(创建) JavaScript 变量

在 JavaScript 中创建变量通常称为"声明"变量。

我们使用 var 关键词来声明变量:

var carname;

变量声明之后,该变量是空的(它没有值)。

如需向变量赋值,请使用等号:

carname="Volvo";

不过,您也可以在声明变量时对其赋值:

var carname="Volvo";

在下面的例子中,我们创建了名为 carname 的变量,并向其赋值 "Volvo",然后把它放入 id="demo" 的 HTML 段落中:

var carname="Volvo";
document.getElementById("demo").innerHTML=carname;

一条语句,多个变量

您可以在一条语句中声明很多变量。该语句以 var 开头,并使用逗号分隔变量即可:

var lastname="Doe", age=30, job="carpenter";

声明也可横跨多行:

var lastname="Doe",
age=30,
job="carpenter";

一条语句中声明的多个不可以赋同一个值:

var x,y,z=1;

x,y 为 undefined, z 为 1。

Value = undefined

在计算机程序中,经常会声明无值的变量。未使用值来声明的变量,其值实际上是 undefined。

在执行过以下语句后,变量 carname 的值将是 undefined:

var carname;

重新声明 JavaScript 变量

如果重新声明 JavaScript 变量,该变量的值不会丢失:

在以下两条语句执行后,变量 carname 的值依然是 "Volvo":

var carname="Volvo"; 
var carname;

JavaScript 算数

您可以通过 JavaScript 变量来做算数,使用的是 = 和 + 这类运算符:

y=5;
x=y+2;

JavaScript 函数定义


JavaScript 使用关键字 function 定义函数。

函数可以通过声明定义,也可以是一个表达式。

函数声明

在之前的教程中,你已经了解了函数声明的语法 :

function functionName(parameters) {
  执行的代码
}

函数声明后不会立即执行,会在我们需要的时候调用到。

函数表达式

JavaScript 函数可以通过一个表达式定义。

函数表达式可以存储在变量中:

var x = function (a, b) {return a * b};

在函数表达式存储在变量后,变量也可作为一个函数使用:

var x = function (a, b) {return a * b};
var z = x(4, 3);

以上函数实际上是一个 匿名函数 (函数没有名称)。

函数存储在变量中,不需要函数名称,通常通过变量名来调用。

Function() 构造函数

在以上实例中,我们了解到函数通过关键字 function 定义。

函数同样可以通过内置的 JavaScript 函数构造器(Function())定义。

var myFunction = new Function("a", "b", "return a * b");

var x = myFunction(4, 3);

实际上,你不必使用构造函数。上面实例可以写成:

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

var x = myFunction(4, 3);

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/littlepage/p/11334937.html