Javascript Learning (1) -- [basic review] variables, declarations, data types, and type conversion

Source: Internet
Author: User
[Javascript] This javascript study mainly targets some javascript basics. Of course, even if you don't know it well, I personally recommend a simple and easy-to-understand book HeadFristJavascript. this book is relatively simple and easy to understand. It is very suitable for javas [javascript] javascript learning. The main object-oriented method is javascript-based. Of course, even if you do not know it well, I personally recommend a simple and easy-to-understand book Head Frist Javascript. This book is simple and easy-to-understand, and is very suitable for beginners of javascript. Haha, let's get started with this book's "advertisement. Before learning javascript design patterns, we should first review the basic knowledge of javascript, which cannot cover all aspects, but can also play a simple recall role. Variable: we can think of it as a container for storing information and naming conventions. It is not mentioned here that Baidu or java programmers should also be clear, and it is case sensitive, Y and y are different. In addition, I think it is worth mentioning that javascript declaration is not as standard as java, it can be said to be a weak type language, that is to say, when you declare a string type, it can be changed in the future. This is indeed very open, but I personally think it is not recommended, because it will make you feel very troublesome. Another point about the variable is that the Declaration uses var declaration, and even if you do not need var declaration, it is a global variable. Data Type: a simple list of commonly used javascript data types. string number boolean array is commonly used in conversion between string and number. Below is a piece of code, as you can see, [javascript]/*** data type and type conversion */(function () {/*** basic data type (3 types) * (1) number * such as 3.1415927, 0.251 ,. 2,100, 1.478E * (2) string * (3) Boolean booble * // numeric conversion string var num1 = 3.1415927; var str1 = Number. toString (num1); document. write (typeof str1 = "string"); // true document. write ("
") // Rounding var num2 = num1.toFixed (2); document. write (num2); document. write ("
") // Return the specified number of digits var num3 = num1.toPrecision (4); document. write (num3); document. write ("
") // (Math) to introduce a method // rounding the round document. write (Math. round (4.7); document. write ("
") // Random origin 0 ~ 1 document. write (Math. random (); document. write ("
") // 0 ~ The random number floor of 10 is rounded down to document. write (Math. floor (Math. random () * 11); document. write ("
") Document. write ("-------------------------------
") // String // note (escape) pca't enter pca \'t \ n line feed/** \ '\ "\ & and number + \\\ n \ r press enter \ t tab \ B Return \ f form * // attribute length indexof substring chartAt (integer) // indexof determines the number of digits of the required string. If no value exists, the value range is-1 // converts it to a number var str2 = "USPCAT. COM "; var str3 =" 3.14 "; var number = Number (str3); document. write (typeof number = "number"); document. write ("
") Document. write (str2-0) +"
"); // NaN non-numeric document. write (str3-1) +"
"); // If it is a subtraction, it will automatically convert the string into a number document. write (str3 + 1) +"
"); // Addition will be treated as String concatenation operations // boolean type (boolean) // true | false var s =" "; var o = {}; // true var l = []; // true var n = null; var f = false; var u = undefined; document. write ("-------------------------------
") If (! S) {document. write ("s is false
")} If (! O) {document. write ("o is false
")} If (! L) {document. write ("l is false
")} If (! N) {document. write ("n is false
")} If (! F) {document. write ("f is false
")} If (! U) {document. write ("u is false
")}/** S is false f is false u is false n is false */if (str! = "" & Str! = Null & str! = Undefined ){//...} if (str ){//...} /*** 2 composite type * (1) array --> ordered set (array): subscript (index) * example * var arr = new Array (); * (2) special object --> function) * // *** special value * (1) null is not a valid object \ array \ string they are empty * (2) undefined indicates that there is no definition and null. It is not a concept. * [No] But there is a container with a box, but there is nothing in the box. * undefined connects to the box and there are no built-in special objects. * // ** * Data Object * Error object * ReExp object */}) () in addition, let's briefly list the usage of the array data type in javascript, and paste a piece of code: [javascript]/*** data type and type conversion */(function () {/*** 2 composite type * (1) array --> ordered set (array): subscript (index) is from 0 * // attribute // constructor returns the function reference to the array that creates this object // index // input // * length // method // * concat merge arrays // * join concatenates arrays according to certain types. // * append the push array // * Delete the last element returned by the pop array // Delete toString shift and return the Array the first element var arr = new Array (); arr. push (1); arr. push (55); arr. push (5); arr. push (3); arr. push (9); // alert (arr. length) var arr2 = [, 45, 8]; // alert (arr2.join (":"); // alert (arr. concat (arr2 ). toString () for (var I = 0; I <arr2.length; I ++) {document. write (arr2 [I] +"
");} // Extend the array method Array. each = function (array, fn) {for (var I = 0; I <array. length; I ++) {fn (array [I])} Array. each (arr2, function (v) {document. write (v +"
") ;}}) () I believe that through this blog, you may have some gains, but it is also possible that there are no special gains. In short, this is not a javascript design designed for beginners. Please understand and review it briefly.

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.