JavaScript basic information

Source: Internet
Author: User

JavaScript Tags:
Two ways to introduce JS:
1.<script type= "Text/javascrip" >JS code </script>
2.<script src= "address/filename. js" type= "Text/javascrip" </script>
JS is either written between the head or written between the body.

Data type:
Variable declaration: var variable name = value;
var can omit variable name = value;
Type: NULL, Boolean, Integer, floating point (single, double), string, array, object, callback, NAN, Infinity (infinity), undefined (undefined)
JS Array (object)
New instantiation
var a = New Array ();
var b = [];
To read an element in an array:
b[Key Name]
Methods in the array
B.push ("Insert Value")
String (object)
var s = New String ();
var w = New String ();
y = "Chinese";
Properties and Methods
Length Leugth (attribute)
B.leugth
JS inside how to Output:
document.write ();

document.write (' ddd ');
Numeric index Array traversal
for (i=0;i<b.length;i++) {
Docuruent.write (B[i])
}
Console output
Console.log ();

Non-numeric indexed array traversal
For (i in B) {
B[i]
}
Declaration of the object
var object = {}; null value
Object = {Property: Property value, Property 2: Property value 2
Mtthod:function (parameter) {method one}
Mtthod:function (parameter) {method two}
};
Determine if a variable is defined
typeof tmp = = ""
var m = {
A: The value of the ' a attribute ',
B: The value of the ' B attribute '
Mtthod:function (A, b) {
if (a>b) {
return A;
}else{
return b;
}
},
Rand:function (A, b) {
Renturn This.max (A, b);
}
};
var B = M.rand (2.6);
Alert (' rand (2,6) = ' +b);

data type conversion;
1. Convert strings to integers: parseint () and Nuber ();
2. Convert strings to Floating point: parsefloat () and Nuber ();
3.eval () function: Executes a piece of JavaScript code represented by a string, such as: Zhang=eval ("+") results are zhang=2;
4. Numeric values are converted to strings: ToString () and string, e.g. b=567; C=b.tostring ();
Note: Use the typeof variable name to view the JS variable data type
5. Convert to Boolean: Boolean (); Boolean ("AAA") result is true;
6.JSON String Conversion:
JSON string: var str1= ' {' name ': ' cxh ', ' sex ': ' Man '} ';
JSON object: Var str2={"name": "Cxh", "Sex": "Man"};
To use the above str1 to convert the JSON string to a JSON object, you must first convert to a JSON object using the following method
var obj=eval (' (' +str+ ') ');

JavaScript basic information

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.