Basic javaScript syntax _ basic knowledge

Source: Internet
Author: User
This article begins with the introduction of javascript, this section describes the javascript syntax and precautions, Dynamic Language, reference of external JS files, variable naming rules, and determination of whether block-level scopes are declared or not, this is an introduction to quite good basic syntax and is recommended to friends. Introduction

JavaScript is a scripting language. (Script, a single text command. During execution, a interpreter of the system translates each piece into a machine-identifiable command and then executes the command. Common scripts: batch processing script, T-SQL script, VBScript, etc .)

HTML is only a markup language that describes the appearance of a webpage. It does not have the computing and judgment capabilities. If all calculations and judgments are performed (for example, it determines whether the text box is empty or whether the two passwords are consistent) if the website is executed on the server side, the page will be very slow, difficult to use, and the pressure on the server will be high, therefore, you must be able to perform some simple operations and judgments in the browser. JavaScript is a scripting language executed on a browser.

JavaScript has no direct relationship with Java. The only relationship is JavaScript, formerly known as LiveScript. Later, it absorbed some Java features and upgraded it to JavaScript. JavaScript is sometimes referred to as JS.

JavaScript is an interpreted language and can be run at any time without compilation.

JavaScript can be used across platforms, as long as there is a browser that supports JS. (Windows, Linux, Mac, Unix)

Syntax and precautions

1) case sensitive: JavaScript is case sensitive. (N and N are two different variables .)

2) for a weak language, declare the variable using var: var num = 10; num = true; yes

3) the string is enclosed in single quotes: var msg = 'stick to output'

4) semicolon after each sentence

5) Js comments, which are the same as C # and Java (// single line comments (recommended) ,/* multi-line comment */(a problem occurs when regular expressions are used )).

Dynamic Language

JS is a very flexible dynamic language. Unlike static languages such as C #, JS in development tools is only an auxiliary and recommended function.

"." Members may not be called, and "." members may not be called. Therefore, do not worry about code problems because they cannot be called. When writing JavaScript, you should be clear about some members of the current object and cannot rely on smart prompts.

Dynamic Language: determines the data type during running.

JavaScript is a dynamic type, so var I = 10; I = 'variable' is valid.

Reference external JS files

In addition to declaring JavaScript on the page, you can also write JavaScript to a separate js file, and then introduce it in the page: Script.

The benefit of declaring a separate js file is that multiple pages can also share and reduce network traffic.

Note:

1) You can write the script tag of the imported external file at the end of the document to improve user experience.

2) You do not need to write the script in the js file.

Variable naming rules

1) starts with a letter, underscore, or $,

2) It can contain letters, numbers, underscores, or $. ($ Is added to the variable name)

3) single quotes are recommended to declare strings.

4) var declaration is recommended for local variables.

Determine whether the statement has been declared

Methods In JavaScript to determine whether declared variables and parameters are initialized (available:
Assume that the existing variable x is:

The Code is as follows:


If (typeof (x )! = 'Undefined' & x! = Null) {alert ("available ");}
If (x) {alert ('variable is available! ');} Else {alert (' variable unavailable! ');} // Null, undefined, '', 0 are regarded as false

Recommendation 2

No block-level scope exists

The scope of the variables declared in the for, while, or if block in the function is within the entire function. To avoid confusion, it is better to declare these variables directly at the beginning of the function.

The Code is as follows:


Var n = 10;
Function ff (){
N ++;
};

Conclusion: JS does not have block-level scope.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.