Getting started with JavaScript and data types

Source: Internet
Author: User
Tags script tag

Tried, JS script in addition to the current page in the title tag, the current page can be put. But it's better to put the rules in place.

A tag href attribute It's good to put a script.

1 <a href= "Javascript:alert (' can also be used in this way);" > Point Me </a>

When using the following method to introduce an external JS file, the script tag cannot contain any code.

1 <script type= "Text/javascript" src= "./bootstrap.min.js" ></script>

Using VAR to declare a variable, a variable creates a space in memory to store the data.

Output variables can also be used with Console.log () and in console input.

document.write () writes content to the body. Document.title () writes content to the title section of the document

JS data type:

1. String type. string is defined by single or double quotation marks, with no difference between quotation marks and single pairs

1 var vzero = ' zero '; var vone  = "This is variable" + vzero;console.log (Vone);

2. Numeric type. Number decimal, integer is numeric type

1 <script type= "Text/javascript" >2     var str = "Hello"; 3     var result = parseint (str);        // use parseint (); You can cast to an integer value 4     Console.log (typeof(Result));       //  Number 5     Console.log (IsNaN (Result));        // true  use isNaN (); You can tell if it is not a value 6 </script>

3. Boolean type. Usually used as a condition to judge, in PHP ' 0 ', ' [] ' is converted into false,js instead.

1 var arr = []; 2 console.log (arr);    // true

4. Function type. Variables can hold functions

 1  <script type= "Text/javascript" >2  var  vone = function   () { 3  }  4   Func () { 5   6  Console.log (vone); // function     7  console.log (typeof  (func)); //  8  </script> 

The scope of the variable in the function. Defining a variable outside of a function is a global variable, whereas a local variable. NOTE: If you declare a variable inside a function, you do not use the var keyword, which means that the variable is a global variable. When a scalar is accessed inside a function, it is searched inside the function, and if it is not found, go to the first-level scope lookup, which is called the scope chain.

1<script type= "Text/javascript" >2     varVone = ' Global ';3     functionfunc1 () {4Console.log (Vone);//unsigned5         varVone = ' func1 ';6Console.log (Vone);//func17     }8 func1 ();9</script>

The parameters of the function. In addition to formal parameters, the actual parameter can also be managed through the arguments object, the first parameter is saved to the first element of the arguments array, the second argument is saved to the second parameter of the arguments array, and so on.

1<script type= "Text/javascript" >2     functionSum () {3 console.log (arguments);4          for(vari = arguments.length-1; I >= 0; i--) {5Console.log (Arguments[i]);//7 8 5 16         }7     }8Sum (1,5,8,7);9</script>

Closure characteristics. When defining functions inside a function, the intrinsic function will include the final value of the variables of the external function, in memory, and some like static variables in PHP.

1<script type= "Text/javascript" >2     functionfunc1 () {3         varV1 = 10;4         varFunc2 =function(){5 Console.log (v1);6         }7V1 = 20;8         returnFunc2;9     }Ten     varresult =func1 (); OneResult ();// - A</script>

Hint: The value inside the function can be read outside of the function.

Use this method to resolve the above problem:

1<script type= "Text/javascript" >2     functionfunc1 () {3         vararr = [];4          for(vari = 3; i > 0; i--) {5Arr[i] =Func2 (i);6         }7         returnarr;8     }9     varFunc2 =function(v) {Ten         returnv; One     } A     varresult =func1 (); -Console.log (result[1]);//1 -Console.log (result[2]);//2 theConsole.log (Result[3]);//3 -</script>

5. Array type. Using arrays, objects can hold multiple values, which can be collectively referred to as composite types of data.

There are two ways of defining an array:

New Array (), shortcut syntax [];

1 New Array (' value1 ', ' value2 ');

Note that you cannot define an array of associative types in JS.

The traversal of an array can be arr.length through a for loop with the subscript to get the data, or directly using for in:

1 var arr = [1,2,3,8,7,5]; 2  for (var in arr) {3    console.log (Arr[num]); 4 }

6. Object type. An array can only define an indexed array, and if you save data of some associated types, you use the object.

Creates an object from the constructor, new object ();

1<script type= "Text/javascript" >2     functionSnake () {//Constructors (constructors)3          This. Name = ' Xiaoqing ';//Object Members4          This. color = ' green ';5          This. Eat =function(){//Object Methods6Console.log (' Eat mouse ');7         }8     }9     varresult =NewSnake ();TenConsole.log (typeof(result));//Object Onealert (result.name);//Xiao Qing AResult.eat ();//Eat Mice -</script>

Create an object by using the shortcut syntax: {} literal

Syntax format:

1 var obname =2{3 Property Name: Property value 4 method Name:function( ) {}5 }

Creates an object from the new object ().

1 <script type= "Text/javascript" >2     varnew  Object (); 3     Obj.name = ' rhubarb '; 4     function () {5         alert (' runing '); 6     }7     obj.eat ();    // runing 8 </script>

You can access the members of an object either by point or by using the [] syntax.

1<script type= "Text/javascript" >2     functionSnake () {//Constructors (constructors)3          This. Name = ' Xiaoqing ';//Object Members4          This. color = ' green ';5          This. Eat =function(){//Object Methods6Console.log (' Eat mouse ');7         }8     }9     varSnake =NewSnake ();Ten      for(varattrinchsnake) { One Console.log (snake[attr]); A     } -</script>

7.null type. A pointer to an empty object that can be understood as a placeholder for an empty object.

1 var NULL ; 2 console.log (typeof(obj));

8.undefined type. The function does not return a value, or the variable is declared, but the value is not initialized, he is undefined

1 <script type= "Text/javascript" >2     function  Snake () {3     } 4     Console.log (Snake ()); 5     var Vone; 6     Console.log (vone); 7 </script>

Getting started with JavaScript and data types

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.