JavaScript Basic Grammar tutorial

  • 2020-12-07 03:57:31
  • OfStack

Basic concepts of Javascript

i. javascript is a scripting language widely used in web pages.

Features of the Javascript language:

1. Object - and event-driven scripting language with defined security developed by Netscape.
2. Features:

Javascript is a scripting language as well as an interpretive language.
Javascript is run by the javascript interpreter in the browser, regardless of the operating environment;
c) in javascript, the data type is not too strict, which has the advantage that it can be more convenient to define or use the data, but it also brings the problem of easy confusion.
Javascript is an object-based language, so you can create your own objects and use the attributes and methods of the objects you create to create a lot of functionality.
e) The main role of Javascript is to make web pages move, but there is also this definite interaction;
f) Javascript is secure and does not allow users to access local hard disks or to modify or delete documents on the network, thus effectively preventing data loss and malicious modification.
g) example: The HTML file has built-in scripts


<script language="javascript"> Document.write("<font color=blue>Hello,JavaScript!</font>"); </script> HTML File add-in script 
<script src="1.js"> </script> //1.js Document.write("<font color=blue>JavaScript,Hello!</font>"); 

JavaScript statement

The JavaScript statement is a command issued to a browser. The statement tells the browser what to do.

A semicolon.

Semicolon is used to separate JavaScript statements.

Usually we add a semicolon at the end of each executable statement.

Another use of semicolons is to write multiple statements in one line.

Tip: You might also see a case without a semicolon.

In JavaScript, ending a statement with a semicolon is optional.

JavaScript code

The JavaScript code (or JavaScript alone) is a sequence of JavaScript statements.

The browser executes each statement in the order in which it was written.

JavaScript code block

The JavaScript statements are combined in blocks of code.

Blocks start with an open curly bracket and end with a close curly bracket.

The block is used to execute sequence 1 of statements.

The JavaScript function is a typical example of combining statements in blocks.

The following example runs a function that operates on two HTML elements:

JavaScript is case sensitive

JavaScript is case sensitive.

When writing the JavaScript statement, be aware that the case switch key is turned off.

The function getElementById is different from getElementbyID.

Similarly, the variable myVariable is different from MyVariable.

JavaScript annotation

JavaScript does not perform annotations.

We can add comments to explain JavaScript, or make the code more readable.

A one-line comment begins with //.

JavaScript multi-line comment

Multiline comments begin with /* and end with */.

JavaScript variable

Like algebra 1, the JavaScript variable can be used to hold values (such as x=2) and expressions (such as z=x+y).

Variables can be short names (such as x and y) or more descriptive names (such as age, sum, totalvolume).

The & # 8226; Variables must begin with a letter

The & # 8226; Variables can also start with the $and _ symbols (though we don't recommend this)

The & # 8226; Variable names are case sensitive (y and Y are different variables)

Tip: both the JavaScript statement and the JavaScript variable are case sensitive.

JavaScript data type

The JavaScript variable can also hold other data types, such as text values (name="Bill Gates").

In JavaScript, a piece of text such as "Bill Gates" is called a string.

There are many types of JavaScript variables, but for now, we'll just focus on numbers and strings.

When you assign a text value to a variable, you should enclose the value in double or single quotes.

Do not use quotes when you assign a value to a variable as a numeric value. If you enclose a value in quotation marks, the value is treated as text.

JavaScript timing event

Using JavaScript, we have the ability to execute code after a specified interval, rather than immediately after a function is called. We call this a timed event.

Using timed events in JavaScritp is easy, and the two key methods are:

setTimeout() executes the code at some point in the future

clearTimeout (cancelled) setTimeout ()

Through the above content, I have introduced the basic grammar related knowledge of javascript to you. I hope it will be helpful for your future work and study.


Related articles: