JavaScript foundation 1 (overview, data types)

Keywords: Javascript Programming Java ECMAScript

JavaScript language foundation


  1. Brief introduction: Javascript (abbreviated as "JS") is famous as a scripting language for developing Web pages. It is the most popular scripting language in the world, but it is also used in many non browser environments. JavaScript is based on prototype programming, multi paradigm dynamic scripting language, and supports object-oriented, command-based and declarative (such as functional programming) styles. Learn JavaScript well PT language is very important!!
  2. History: Javascript was invented by Brendan Eich of Netscape (Netscape communication company) in 1995. It was first designed and implemented on Netscape Navigator browser. At first, it was named LiveScript. Because Netscape cooperated with Sun company, Netscape management wanted it to look like Java, so it was named JavaScript. ECMA (European Association of computer manufacturers) has developed ECMAScript standard based on JavaScript; the latest version has reached es6, but most browsers only support es5 code.
  3. Introduce JavaScript language:
  • Internal label
   //Code content
//Do not display the definition type, which is javascript by default
<script type="text/javascript">
   //Code content
  • External introduction
<script src="test.js">  </script>

Basic grammar

  1. Value: js does not distinguish between decimal and integer, Number
1234      //Integer 123
1234.12   //Floating point 123.1
1.23e45   //Scientific counting
-1234     //complex
NaN       //not a number
Infinity  //Infinity
  1. Character string
// Normal strings are wrapped in single quotation marks or double quotation marks
\n; //Line feed
\t; //One character space
str.length; //Length of string
//Multiline string writing
var msg = 
//World '

Case conversion:

//This is the method, not the property

String truncation:

student.substring(1)   //From the first character to the last
student.substring(1,3) //Intercept the first and second character, and the interval represents [1,3]
  1. Boolean value
  1. operator

Arithmetic operator

+	//addition	
-	//subtraction	
*	//multiplication	
/	//division	
%	//Remainder	
++	//Self increment	
--	//Self decrement

Logical operators

&&   //And operator: both are true and the result is true
||   //Or operator: one is true and the result is true
!    //Non operator: true is false, false is true

Assignment Operators

=    x=y  //Assign y value to x
+=   x+=y //Namely: x=x+y
-=   x-=y //Namely: x=x-y
*=   x*=y //Namely: x=x*y
/=   x/=y //Namely: x=x/y
%=   x%=y //Namely: x=x%y

For Strings: connect two or more string variables using the + operator
s3: HelloWorld
If you need to add a space to the connected string, you need to insert the space into a string:
s1="Hello ";
s3: Hello World

Comparison operator

==/ / equal to var x=5; x==8; the result is false
 ==="/ / absolutely equal to (equal value and type) var = 5; X = =" "5" "results in fasle var = 5; X = = 5; results in true"
! = / / not equal to
 ! = = / / not absolutely equal (either one value and type are not equal, or both are not equal)

Be careful:
NaN = = NaN, this is not equal to all the values, including yourself. You can only judge whether this number is NaN through isNaN(NaN)

  1. Array: Java values must be objects of the same type, which is not required in js
//Array can contain any data type
var arr = [1,2,3,4,'hello',null,true];
new Array(1,12,3,4,4,5,'hello');
var arr = [1,2,3,4,5,6];
arr.length //Array length
arr.indexOf(2) //Get index of subscript through element
arr.slice(1,3) //Intercept the elements between the arr subscripts 1 and 2, and return a new array, similar to the substring in String
push() //Press in to tail
pop()  //Pop an element at the end

unshift() //Press in to head
shift()   //Pop an element of the head

arr.sort()//Array sorting is case insensitive
var arr2 = ["B", "C", "A"]
["A", "B", "C"]

arr.reverse() //Array inversion
var arr3 = ["A", "B", "C"]
["C", "B", "A"]

concat() //Connect two or more strings
var arr4 = ["C", "B", "A"]
["C", "B", "A", 1, 2, 3] //concat() does not modify the array, but returns a new array
join()  //Connector
var arr5 = ["C", "B", "A"]

//Multidimensional array
var arr6 = [[1,2],[3,4],["5","6"]];
arr6[1][1] //Value

Take array subscript: if there is out of bounds, undefined will appear

  1. object
var person = {
    name: "Zhang San",
    age: 20,
    tags: ['aaa','bbb','ccc','...']
//Take the value of the object
//Zhang San
//Object Assignment = "Li Si"
//Judge whether the object attribute exists
'age' in person
//Determine whether a property is owned by the object itself
//Delete the properties of an object
  1. Check the format strictly: prevent some problems caused by the randomness of JavaScript
    Premise: IEDA needs to support ES6 syntax
'use strict'   //Must be written in the first line of JavaScript
//When you define variable i and use strict check format, i in the following code will turn red. let is recommended to define all local variables
i = 1;
  1. Process control

if judgement

var age = 3;
if (age>3){ //First judgment
   alert("aaa"); //alert: pop up
}else if(age<5) { //Second judgment
}else {  //otherwise

while cycle

while(Judgement condition){


}while(Judgement condition)

for cycle

for (let i = 0; i < 100 ; i++) {

forEach cycle

var arr = [1,2,3,4,5,6,7,8]
      arr.forEach(function (value) { //function

for in loop

var arr = [1,2,3,4,5,6,7,8]
for (var num in arr){
        if (arr.hasOwnProperty(num)){
  1. map set
    New features of ES6
//map: get value by key
        var map = new Map([['tom',100],['jack',90],['haha',80]]);
        var name = map.get('tom'); //Get value through key!
        map.set('admin',123456);  //Add or modify!
        map.delete("tom"); //Delete!
        //let of
        for (let x of map){

//Set: an unordered and distinct set
        var set = new Set([1,2,3,4,1])
        //Set(4) {1, 2, 3, 4} / / four elements are {1, 2, 3, 4}
        set.add(2);     //Add to!
        set.delete(1);  //Delete!
        //Set(3) {2, 3, 4} / / the three elements are {2, 3, 4}
        set.has(3);     //Include an element or not!
        //Ergodic let of
        for (let x of set) {

//Goodbye Thank you

33 original articles published, 21 praised, 1118 visited
Private letter follow

Posted by sbourdon on Tue, 04 Feb 2020 08:08:30 -0800