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);
parseFloat(num_str);
2. Text common operation API
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. |
var str = 'If life deceives you';
str.charAt(0);
str.indexOf('living');
str.lastIndexOf()
str.substring(2,6);
str.replace('Life','world');
var new_str = 'Ha-ha,Hee hee,Ha-ha,Hey';
new_str.split(',');
new_str.split('/');
Three, array
1. Array definition
var array = ['a','b','c','d'];
array.length;
array[1];
2. Array operation
- Insert or overwrite array elements
var array = ['a','b','c','d'];
array[4] = '8';
array[3] = 2;
array[8] = 'test';
var array = ['a','b','c','d'];
array.push('d','e','f');
array.unshift(0,1,2);
var array = ['a','b','c','d'];
array.pop();
array.shift();
delete array[1];
array.splice(1);
var arr1 = ['a','b'];
var arr2 = ['c','d'];
var arr3 = arr1.concat(arr2);
4, Basic process control
1. branch
if() {
} else {
}
if() {}
else if() { }
else {}
switch() {
case condition1 : statements1; break;
case condition2 : statements2;break;
default : statements3;break;
}
2. cycle
while() {
statements...
}
var i = 0;
while(i < 10) {
i++;
console.log(i);
}
for(var i = 0; i < 10; i++) {
console.log(i);
}
Keywords:
Javascript
less
Added by ofirf96 on Wed, 01 Jan 2020 04:27:51 +0200