Introduction to JavaScript basic syntax

Source: Internet
Author: User

Introduction

JavaScript is a scripting language. (script, a text command of all stripes.) Execution is performed by an interpreter of the system, translating one of its strips into a machine-recognizable instruction. Common scripts: Batch scripts, T-SQL scripts, VBScript, and so on. )HTML just describes the appearance of the Web page markup Language, no calculation, judgment, if all the calculations, judgments (such as whether the text box is empty, to determine whether the two passwords are input consistent) are placed on the server side of the Web page will be very slow, use it is very difficult to use, the pressure on the server is also very large, Therefore, it is required to perform some simple operations and judgments in the browser. JavaScript is a scripting language that executes on the browser side. JavaScript and Java are not directly related, the only relationship is JavaScript formerly known as LiveScript, later absorbed some Java features, upgrade to JavaScript. JavaScript is sometimes referred to as JS. JavaScript is an interpreted language that can be run at any time without compiling. JavaScript can be cross-platform, as long as there is a JS-enabled browser. (Windows, Linux, Mac, Unix)

syntax and considerations

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

2) Weakly typed language, declaring variables with Var:var num=10;num=true;

3) string with single quote: Var msg= ' insist on not leaking '

4) semicolon after each sentence

5) JS annotation, the same as C #, Java (//single-line comments (recommended),/* Multiline comment */(there is a problem when using regular expressions)).

Dynamic Language

JS is a very flexible dynamic language, not like C # and other static language as rigorous, development tools in the JS completion function is only a support, suggestions.

“.” Out of the member invocation may not be available, "." A member who does not come out may also be able to invoke it, so do not worry about the code because it is "out of the question". When writing JS, you should be aware of the current object some of the members, can not rely on smart tips.

Dynamic language: Determines the data type at run time.

JavaScript is a dynamic type, so var i=10;i= ' variable ' is legal.

referencing external JS files

In addition to the ability to declare JavaScript in the page, JavaScript can be written to a separate JS file and then introduced in the page: <scriptsrc= "test.js" type= "Text/javascript" >< /script>.

The benefit of declaring to a separate JS file is that multiple pages can also be shared, reducing network traffic.

Attention:

1) The <script> label of the imported external file can be written at the end of the document to improve the user experience.

2) JS file does not need to write <script>

variable naming rules

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

2) The middle can include letters, numbers, underscores, or $. (more than one $ in variable naming)

3) It is recommended to use single quotation marks to declare a string

4) Recommended VAR declaration for local variables

Determine if you have declareda method in JavaScript that determines whether a variable is declared or initialized (available):

Suppose you already have a variable x:

if (typeof (x)! = ' undefined ' && x!=null) {alert ("available"); }

if (x) {alert (' Variable available! ’); } else {alert (' variable is not available!} ’); }//null, undefined, ' ', 0 are considered false

Recommend a second

There is no block-level scope for variables declared within a for or while or if block within a function, and the scope of the scoped range is the entire function, so to avoid confusion, declare the variables directly at the beginning of the function. Var n=10; function ff () {n++;}; Conclusion: There is no block-level scope in JS.


Introduction to JavaScript basic syntax

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.