JavaScript foundation 1 (overview, data types)

Keywords: Javascript Programming Java ECMAScript

JavaScript language foundation

Summary

  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
<script>
   //Code content
</script>
//Do not display the definition type, which is javascript by default
<script type="text/javascript">
   //Code content
</script>
  • 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
'abcdefg'
"abcdefg"
\n; //Line feed
\t; //One character space
str.length; //Length of string
//Multiline string writing
var msg = 
    `hello
world
//Hello
//World '

Case conversion:

//This is the method, not the property
student.toUpperCase()
student.toLowerCase()

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
true;
false;
  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
s1="Hello";
s2="World";
s3=txt1+txt2;
s3: HelloWorld
If you need to add a space to the connected string, you need to insert the space into a string:
s1="Hello ";
s2="World";
s3=txt1+txt2;
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
6
arr.indexOf(2) //Get index of subscript through element
1
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
[2,3]
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"]
arr2.sort() 
["A", "B", "C"]

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

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

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

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
person.name
//Zhang San
//Object Assignment
person.name = "Li Si"
//Judge whether the object attribute exists
'age' in person
true
//Determine whether a property is owned by the object itself
person.hasOwnProperty('age')
true
//Delete the properties of an object
delete person.name
  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
   alert("bbb");
}else {  //otherwise
   alert("ccc");
}

while cycle

while(Judgement condition){

}

do{
    
}while(Judgement condition)

for cycle

for (let i = 0; i < 100 ; i++) {
    console.log(i)
}

forEach cycle

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

for in loop

var arr = [1,2,3,4,5,6,7,8]
for (var num in arr){
        if (arr.hasOwnProperty(num)){
             console.log(arr[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){
            console.log(x)
        }

//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!
        //true
        //Ergodic let of
        for (let x of set) {
            console.log(x)
        }

//Goodbye Thank you

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

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