JavaScript Basics - basic data types and basic process control

JavaScript foundation 1

1, Basic data type

1.Number
2.String
3.Null
4.Undefined
5.true or false

2, Common methods of string

1. Parse string number
  • parseInt: resolve to integer
  • parseFloat: parse to decimal
var num_str = '4.5 Kilogram';
parseInt(num_str);//Return to 4
parseFloat(num_str);//Return to 4.5
2. Text common operation API
  • Method description
Method Explain
charAt(index) Returns the specified character in the string according to the subscript
indexOf(") Returns the subscript in the string based on the specified character
substring() Intercepts the string based on the subscript. Pass in the start and end subscripts as parameters. But the returned string does not contain the character ending the subscript. That is, "including the head but not the tail".
replace() Replaces the specified character or string.
split() Splits the string according to the specified separator and returns an array.
  • Example
var str = 'If life deceives you';
str.charAt(0);//Return to 'false'. '' if the subscript is out of the string range.
str.indexOf('living');//Return to 2. Returns - 1 if the character is not in the string.
str.lastIndexOf()//Is to find the subscript of the first character from the back to the front
str.substring(2,6);//Return to 'life deception'. It is the same result as str.substring(6,2).
str.replace('Life','world');//Return to "if the world deceives you.".

var new_str = 'Ha-ha,Hee hee,Ha-ha,Hey';
new_str.split(',');//Return to ["ha ha", "ha ha", "ha ha", "ha ha"].
new_str.split('/');//If the specified delimiter is not in the string. Then return to ["ha ha, ha ha, ha ha"]

Three, array

1. Array definition
  • var array = [];
var array = ['a','b','c','d'];
array.length;//Return to 4
array[1];//Return to'b'
2. Array operation
  • Insert or overwrite array elements
var array = ['a','b','c','d'];
array[4] = '8';//array is now ['a','b','c','d',8];
array[3] = 2;//The "d" with the subscript 3 is overwritten. array is ['a','b','c',2,8]
array[8] = 'test';//array is ['a','b','c',2,8,empty x 3,'test '];
  • Append array element
var array = ['a','b','c','d'];
//Append from the end of the array.
array.push('d','e','f');//array is ["a", "b", "c", "d", "d", "e", "f"]
//Append from array.
array.unshift(0,1,2);//array is [0, 1, 2, "a", "b", "c", "d"]
  • Delete array element
var array = ['a','b','c','d'];
//Remove from end of array
array.pop();//array is ['a','b','c '];
//Delete from array
array.shift();//array is ['b','c'];
//Delete element value of array specified subscript
delete array[1];//array is ['b',empty];
//Completely delete the elements of the array according to the subscript
array.splice(1);//array is ['b '];
  • Merged array
var arr1 = ['a','b'];
var arr2 = ['c','d'];
var arr3 = arr1.concat(arr2);//arr3 is ['a','b','c','d '];

4, Basic process control

1. branch
  • if
/**
*Single condition judgment
*/
if() {
   //True execution in parentheses
   } else {
   //False execution in brackets
   }
/**
*Multiple condition judgment
*/
if() {}
else if() { } 
else {}
  • switch
switch() {
       case condition1 : statements1; break;
       case condition2 : statements2;break;
       default : statements3;break;
       }
2. cycle
  • while
while() {
      //Execute when bracket is true
      statements...
      }
//Cycle numbers 1-10
var i = 0;
while(i < 10) {
    i++;
    console.log(i);
} 
  • for
for(var i = 0; i < 10; i++) {
    console.log(i);
}
//Description of execution sequence
//1. Define i=0. 2. Determine whether it is less than 10, and then execute method block printing i. i is now 0. 3. Execute i + +, i is 1.
//Then judge whether it is less than 10, print again, and + +... Until I < 10 is false.

Keywords: Javascript less

Added by ofirf96 on Wed, 01 Jan 2020 04:27:51 +0200