JavaScript Basic Syntax

Source: Internet
Author: User
Tags case statement

First, the basic grammar
    • 1. Basic syntax and variables
    • 2. Data type
    • 3. Tips Summary

1. Basic syntax and variables

1.1 Statements

JavaScript is a line of execution (line), in general, each row is a statement, the statement (statement) separated by semicolons (;)

Statement: An operation performed in order to accomplish a certain kind of task. For example:

vara=1+b;

Expression: The formula to get the return value. For example:

1+3

The expression does not need the semicolon to divide, has the semicolon isolation to become the statement;

1.2 Variables

A variable is a reference to a value, and a variable is equivalent to referencing a value each variable has a variable name.

Declaration of variables: using var for example: Var a

Assignment of variables: using var for example: var a = 1 (by = establishing a reference relationship)

Declares a variable without assigning a value, at which time the value of the variable is undefined; Javascirpt is a dynamic type language, which means that there are no restrictions on the types of variables that can be assigned to various types of values.

1.3 Variable Promotion

The JavaScript engine works by parsing the code, getting all the declared variables, and then running on a line. The result is that all declarations of variables will be promoted to the head of the code, which is called variable elevation

Console.log (a); var a = 1; at this point, the output is undefined, and does not report a undefined error.

1.3 identifiers

    • The first character can be any Unicode letter, as well as the dollar sign ($) and underscore (_).
    • The second character, followed by a character, can also be used as a number.

Note: Reserved words cannot be used as variable names.

There are two ways to annotate javascript: one-sentence comment//; Text Comment/*....*/

1.4 Statements (conditional statements and loop statements)

1. If statement (conditional statement)

1. If (expression){....} 2. if (expression) {}else if (expression) {}...else{};

2. When multiple if...else are used together, it can be converted to a switch statement

Switch(expression){switch (expression) Case value1: ... break; .... Default: ...}

Note that the expression after the switch statement is the one that follows the case statement, and when you compare the run result, the strict equality operator (= = =) instead of the equality operator (= =), which means that no type conversion occurs when the comparison is made. The switch structure is not conducive to code reuse, and can often be overridden in object form.

varF={Work :function () {return},Shop :function () {return},read:function () {return}}

3. Looping statements

1.while (expression) {} and Do{}while (expression)
2.for (initialize,test,increment) {}
3. Lable: For example: Go:
tip: Break: End current loop of current layer; Continue: skip this loop (loop)

2. Data type

2.1 Overview

Each value of a JavaScript language belongs to a data type. There are six categories and two special values for JavaScript data types.

Six categories of data types can be divided into two groups: primitive type (primitive type) and composition type (complex type).

1. The original type consists of three data types.

Numeric: typeof 1//number
String: typeof "1"//String

Boolean value (Boolean)//typeof false//false

2. Composition type also includes three data types (typeof ComplexType//Object)

Object: Use the instanceof keyword to determine var a = {} A Instanceof object//True

Arrays (Array)

Functions (function)

Objects and arrays are two different ways of combining data, and functions are actually methods of working with data. In addition to the above six categories, JavaScript also defines two special values null and undefined.

   if  (typeof Span class= "NX" >v ===  "undefined" ) {//... }              
Span style= "color: #000000; Background-color: #ffffff; " > typeof window //"Object"
typeof {} // "object"typeof [] // "object"typeof null // "object"

3. Tips Summary

> Use if statement always with {}

> Determine if a variable is defined, using the keyword typeof

> statements are appended with semicolons (;).

JavaScript Basic Syntax

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.