JavaScript basic Operations

Source: Internet
Author: User

first, how to write? 1.JavaScript code existence form
<!--way one--><script type= "Text/javascript" src= "js file" ></script>   <!--mode two--><script Type= "Text/javascript" >    
2.JavaScript storage location

The script in the HTML must be between the <script> and </script> tags.

The script can be placed in the <body> and

Because the HTML code is executed from top to bottom, if the JS code in the head is time consuming, it will cause the user to be unable to see the page for a long time, if it is placed at the bottom of the body code block, even if the JS code is time consuming, it will not affect the user to see the page effect.

Here, the recommendation is placed at the bottom of the body body;

3. Notes

JavaScript does not execute annotations.

We can add comments to explain JavaScript, or to improve the readability of the code.

Single-line comments begin with//.

Output title: document.getElementById ("myH1"). Innerhtml= "Welcome to my homepage";//Output paragraph: document.getElementById ("MyP"). Innerhtml= " This is my first paragraph. ";

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

/* The code below will output a heading and a paragraph that will represent the start of the home page */

Second, the variable

The declaration of a variable in JavaScript is a very error-prone point, a local variable must start with a Var, and if Var is not used, the default is to declare a global variable.

JavaScript variables can be used to hold values (such as x=5) and expressions (such as z=x+y).

var x=5;var y=6;var Z=x+y;

Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).

    • Variables must start with a letter
    • Variables can also start with the $ and _ symbols (although we do not recommend this)
    • Variable names are case sensitive (Y and y are different variables)

Both JavaScript statements and JavaScript variables are case-sensitive.

<script type= "Text/javascript" >      //global variable    name = ' Seven ';      function func () {        //local variable        var age =;          global variable        gender = "Male"    }</script>

third, the data type

The data types in JavaScript are divided into primitive types and object types:

    • Original type
      • Digital
      • String
      • Boolean value
    • Object type
      • Array
      • Dictionary
      • ...
1. Digital

In JavaScript, integer values and floating-point values are not distinguished, and all numbers in JavaScript are represented by floating-point numbers.

Transformation:

    • parseint (..) Converts a value to a number, or Nan if unsuccessful
    • Parsefloat (..) Converts a value to a floating-point number, or Nan if unsuccessful

Special values:

    • NaN, not a number. Can be judged using IsNaN (num).
    • Infinity, infinitely large. Can be judged using isfinite (num).

2. String

A string is an array of characters, but in JavaScript the string is immutable: You can access text anywhere in the string, but JavaScript does not provide a way to modify the contents of a known string.

Common features:

Obj.length Gets the current string length
Obj.trim () remove whitespace
Obj.trimleft ()
obj.trimright)
Obj.charat (n) returns the nth character in a string
obj.concat (value, ...) stitching
Obj.indexof (Substring,start) sub-sequence position
Obj.lastindexof (Substring,start) sub-sequence position
obj.substring (from, to) gets the---starting position and ending position of the subsequence according to the index
Obj.slice (start, end) Slice
obj.tolowercase () uppercase
obj.touppercase () lowercase
obj.split (delimiter, limit) split
Obj.search (regexp) matches from the beginning, returning the first position where the match was successful (g invalid)
Obj.match (regexp) Global Search, if there is a G in the regular means find all, otherwise only find the first one.
Obj.replace (regexp, replacement) is replaced, and g in the regular replaces all, otherwise only the first match is replaced .
$ number: matches the nth group content;
$&: current matching content;
$ ': text located to the left of the matched substring;
$ ': text on the right side of the matching substring
$$: Direct volume $ symbol

3. Boolean type

The Boolean type contains only true and false, unlike Python, whose first letter is lowercase.

    • = = Compare Values equal
    • ! = does not equal
    • = = = Comparison value and type are equal
    • !== Not equal to
    • || Or
    • && and

4. Arrays

The array in JavaScript is similar to the list in Python.

Common features:

1234567891011121314 obj.length          数组的大小obj.push(ele)       尾部追加元素obj.pop()           尾部获取一个元素obj.unshift(ele)    头部插入元素obj.shift()         头部移除元素obj.splice(start, deleteCount, value, ...)  插入、删除或替换数组的元素                    obj.splice(n,0,val) 指定位置插入元素                    obj.splice(n,1,val) 指定位置替换元素                    obj.splice(n,1)     指定位置删除元素obj.slice( )        切片obj.reverse( )      反转obj.join(sep)       将数组元素连接起来以构建一个字符串obj.concat(val,..)  连接数组obj.sort( )         对数组元素进行排序

Iv. for loops and conditional statements

1.for Cycle

When looping, the elements of the loop are indexed

<! DOCTYPE html>

Execution Result:

JavaScript basic Operations

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.