JavaScript - I found that you are such a JS (Basic Concept - Soul Chapter, let's learn JS together)

introduce

This is the third reading note (introduction to soul) of the Red Treasure Book (3rd edition of JavaScript Advanced Programming). It has the knowledge content of the remaining 3rd chapter. Of course, it also has my own understanding. Red Treasure Book can be said to be difficult to gnaw, it is not easy to read, rather thick, to understand is not easy, to be proficient in js is even more difficult, the need for constant accumulation and review. Therefore, it is not enough to read the book once or twice. It needs to be read more. It can be said that learning from the past and learning from the new can be a teacher.

Many people read these thick books for three days, fishing and netting for two days. They seldom read them. What they can remember is often the content of the first three chapters. So I wrote a blog to read this book and learn js with you.

Warm Tip: Note words will have subjective color, knowledge key record. Blog content-based, suitable for beginners.

Basic Concepts (Soul Chapter)

The last one is about the body, and the next one is about the connotation (data types and operators). In Chinese, it's like letting you understand words and meanings. The meaning of sentences is like injecting these words and sentences into your soul.
Same series:
First article: JavaScript - I found that you are such a JS (first knowledge, source)
Second article: JavaScript - I found that you are such a JS (basic concept - body, may as well look at JS from the Chinese perspective)

data type

js has six kinds of data structures, five basic types and one complex type. js does not support any custom data types, so these six are enough.

Five basic data types:

  • Undefined
  • Null
  • Boolean
  • Number
  • String

One complex type:

  • Object type

1.1 typeof operator

This operator can detect what data type a variable is.

The value returned by typeof:

  • "Undefined" - means that the value is undefined
  • "String" -- means that this is a string
  • "number" - indicates that this is a numeric type
  • "boolean" -- Represents a boolean type
  • "Object" -- means that this is an object
  • "Function" -- means that this is a function

Specifically: for null, typeof null returns "object" because null represents a reference to an empty object

Use examples of typeof:

var str = "hello";
var a;
console.log(typeof str)  //"string"
console.log(typeof 666)  //"number"
console.log(typeof a)  // "undefined"
console.log(typeof null)  // "object"

1.2 Undefined type

The undefined type has only one value, undefined. The default value is undefined when a variable is declared with var but not initialized.

var value ;
console.log(typeof value);   // "undefined" 

//Specifically: For undefined variables, typeof returns undefined values, such as:
//The following a is not declared
console.log(typeof a);    //"undefined"

1.3 Null type

Null type also uses only one value, null, which is an empty object pointer. So when typeof detection is used, the object is returned.

console.log(typeof null);   //"object"

Note:

1. Use of null: When a defined variable will be used to save an object, it is initialized as null, so that we know that the variable will refer to an object in the future.

2. In fact, undefined values are derived from null, so an equality test on them returns true:

console.log(null == undefined);  //true

1.4 Boolean Type

boolean type has two values true and false, and other types of values have the same value as these two values, that is to say, other types of values can be converted into one of these two values. Through the transition function Boolean()

Note:

Boolean() method can be called for any type of data, and a true or false can be returned. As for what to return, there are some rules, as follows:

//For Undefined
console.log(Boolean(undefined));  //false
//For String, non-empty strings return true and empty strings return false
console.log(Boolean(""));   //false
console.log(Boolean("hahaha"));   //true
//For Numble types, any non-zero number returns true, and 0 and NaN returns false.
console.log(Boolean(123));  //true
console.log(Boolean(0));  //false
//For Object, any object returns true
console.log(new Array());  //true

1.5 Number type

Number type is the type of number, all kinds of numbers are of this type, including integers, floating-point numbers.

Common numbers:

//integer
var intNum = 10;
//Octal 0 begins
var num1 = 066;
//Hexadecimal 0x Beginning
var num2 = 0xA;
//Floating point number
var floatNum = 0.6;
//The largest number is 4123000, using the scientific representation e, e6 to represent the sixth power of 10.
var floatNum2 = 4.123e6;
//Maximum value
Number.MAX_VALUE;   //1.7976931348623157e+308
//minimum value 
Number.MIN_VALUE;   //5e-324
//When exceeding the maximum, it is expressed as positive infinity.
Infinity
//When exceeding the minimum, it is expressed as negative infinity.
-Infinity
//NaN (Not a Number) Non-numerical
NaN
//isNaN()
console.log(isNaN(10))  //false
console.log(isNaN("10")) //false,isNaN() tries to convert a string to a number, where "10" can be converted to 10
console.log(isNaN("bey"));  //True, "bey" cannot be converted to a number

1.6 String type

String is used to represent a sequence of characters consisting of zero or more 16-bit Unicode characters, that is, strings.

Strings are enclosed by single quotation marks ('') or double quotation marks ('). But the beginning and end can not be different quotation marks, such as a single pair.

Wrong point:

//This will lead to errors.
var str = 'javasritp";

Characteristic:

1. Stitching strings with'+'numbers

var str1 = "hello";
var str2 = str1 + "world";
console.log(str2); // "hello world"

2. Other types of data are converted to strings using the toString() function

var num = 123;
var str = num.toString();  //Number 123 is converted to a string by toString
console.log(str);  //"123"
//Stitching other data types with "" can be converted to String
var s = 23+"";  //"23"

1.7 Object Type

Objects in ECMAScript are actually a collection of data and functions. Create by following the new operator with the name of the object type to be created.

//Create an Object object
var  o = new Object();

Key:

Objects here are like ancestors, and creating instances of Objects is of little use. It's like a java.lang.Object object in java.

Characteristic:

Attributes and methods common to instances of each Object type:

  • constructor: saves the function used to create the current object.
  • hasOwnProperty: Used to detect whether a given property exists in an instance of the current object.
  • IsPrototype Of: Used to check whether the incoming object is a prototype of the current object?
  • propertyIsEnumerble: Used to check whether a given attribute can be enumerated using for-in
  • toLocaleString(): The string representation of the returned object.
  • toString(): The string representation of the returned object.
  • valueOf(): Returns the string, value, or Boolean representation of the object. Usually the same value is returned as toString().

Operator (Operator)

Next is the operator that operates on the data value. Contains arithmetic operators, bitwise operators, relational operators, and equality operators.

1 unary operator

Operators that can only operate on one value are called unary operators.

1.1 Increase and decrease:

Form: This is self-increasing and self-decreasing, using two plus signs (++) or two minus signs (--).

var num = 5;
num++;   //num = num+1; num = 6;
num--;   //num = num-1; num = 5;

Notes: Incremental subtraction of the pre-type (plus and minus sign before the value) and post-type (plus and minus sign after the value);
1. No difference: When there is only one incremental or decreasing operation, the results are not affected.

var num1 = 5;
var num2 = 5;
num1++;   //After execution, the value of num1 becomes 6
++num2;   //The value of executing num2 becomes 6

2. Differences: When operands are not just incremental or decreasing in a statement

var val1= 5;
var val2 = 5;
var val3 = 20;
var sum1 = val1-- + val3;   //sum1 equals 25
var sum2 = --val2 + val3;   //sum2 equals 24
//The values of val1 and val2 are the same here, but the final results are different.
//Val1 is postpositioned, and in general, it is only at the end of the operation to reduce itself. So the order is that the original value of val1 (5) and val3 are added together and then assigned to sum1, and finally val1 is reduced by itself.
//val2 is the front-end, the most popular point is the front-end, the first self-reducing operation. So the order is val1 minus itself, then add up with val3 and assign the final value to sum2.

1.2 Univariate plus sign and minus sign

Form: Use a plus or minus sign before a value.

var num = 100;
//Use + positive or positive, negative or negative
+num;  //100
//Use minus sign to turn into opposite number
-num;  //-100

Note: When the operation is not a number, the use of +, - enables it to perform conversion, similar to the Number() function.

//For + No.
var str = "10";
str = +str;  //The value becomes the number 10
var str2 = "hello";
str2 = +str2 //The value is NaN because it cannot be converted to a number
var boo = false;
boo = +false;  //The value has changed to 0.

//For the minus sign, it's almost the same, just the opposite number.
var s = "100";
s = -s; //The value is -100

2. Additive/multiplicative operators (addition, subtraction, multiplication, division, modulus)

1. Plus (+): Addition
2. Subtraction sign (-): subtraction operation
3. Multiplication (*): Multiplication
4. Divisor (/): divide
5. Module (%): Residual

30+20;  //50
30-10;  //20
30*20;  //600
30/10;  //3
25/6 ;  //1`

3. Boolean operators

Boolean operators have three: non, and, or

3.1 logical non

1. symbol:!
2. Function: Non-operation returns a Boolean value. Function takes a non-Boolean value, that is, the opposite. It converts a pair of values to a Boolean value.

console.log(!false);  //true
console.log(!0);  //The Boolean transformation of 0 is false, and after non-operation it is true.
console.log(!"hh")  //The non-empty string Boolean is true and false after non-operation

//Tips, use two!! Can be similar to Boolean function, Boolean conversion of any value.
console.log(!!"hello");  //true
console.log(!!0);  //false

3.2 logic and

1. Symbols:&&
2. Function: Two values are needed to perform and operate. True is returned only when both Boolean values are true, otherwise false is returned, and it is a short-circuit operator (when the first value is false, the second value is no longer judged).

console.log(false&&true); //false
console.log(false&&false); //false
console.log(true&&false); //false
console.log(true&&true); //true

3. Note: && (and) The operation does not necessarily return the Boolean value. This is a special case:

  • If the first operand is an object, return the second operand
  • If the first operand is null, return null
  • If the first operand is NaN, return NaN
  • If the first operand is undefined, return undefined.

    //Array objects
    var arr = new Array();
    //Character string
    var str = 'hello';
    console.log(arr&&str) //'hello'
    console.log(null&&str) // null
    console.log(NaN&&str)  //NaN
    console.log(undefined&&str) //undefined

3.3 logic or

1. Symbols:||
2. Function: It needs two values to perform and operate. If there is a Boolean value of true, it returns true. Otherwise, false or operation is also a short-circuit operator. When the first operation value is true, it returns true directly without checking the second operation value.
3. Note: If an operand is not a Boolean value, the | | or operation does not necessarily return a Boolean value. This is a special case:

  • If the first is an object, the first operand is returned
  • If the first number is false, the second number is returned.
  • If both numbers are objects, return the first operand
  • If the two operands are NaN, return NaN
  • If the two operands are undefined, return undefined.
var arr  = new Array();
console.log(arr||"str");  //Returns an array empty array []
console.log(NaN||NaN) ; //NaN
console.log(false || "str") //"str"

4 relational operators

1. Relational operators are greater than (>), less than (<), less than or equal to (<=), greater than or equal to (>=).
2. Attention:

//1. For numerical conversion, compare directly and return true or false
34>23;  //true
//2. For the comparison of two strings, the size of their character encoding values (ascll codes) is compared one by one from the beginning to the end.
"Biasd"<"adsf";  //true, because B character encoding is 66 less than a character encoding 97
//3. For string-to-number comparisons, strings are converted to numbers
"23"<3;  //false, 23 turns to 23, and then compares with 3
"a"<3 //False, "a" to NaN, NaN to any operand comparison is false

5. Equality operator

Equality operators have equality (==) and inequality (!=), equality (==) and inequality (!=).

Equality and inequality are first converted and then compared, while equality and inequality are only compared and not converted.

5.1 Equal and unequal transformation emphasis:

//1. If an operation value is Boolean, the Boolean value is converted to a value, true to 1, false to 0.
true == 1 ;  //true
//2.null and undefined are equal
null == undefined ; //true
//3. When an operand is NaN, it must return false.
NaN == 1;  //false
//4. Two NaN comparisons also return false
NaN == NaN ;  //false
//5. When two values are objects, return false if they are not directed at the same object

5.2 Equal and Incomplete Focus:

It can be seen that all equals are three equal signs, so they are more strict and will not be converted as equals. Popular saying: you are not, you are not.

//A comparison of equality and equality
"55" == 55 ;  //true, in the same case, "55" can be converted
"55" === 55 ;  //False, all-equal case, "55" does not convert to a number, so the data type is different and returns false directly.

//For null and undefined, the two values are equal when the above equal comparison is made, but the full comparison is not equal.
null === undefined ;  //false

6. Conditional operators (ternary operators)

1. Template: expression 1? Expression 2: expression 3.
2. Instructions: When expression 1 is true, return the result of expression 2 before the colon, and false return expression 3 after the colon.

//Example:
var num = 3>2?1:0;   //num has a value of 1

7. Assignment operator

1. Simple assignment operations are represented by (=) numbers

var val = 10;  //Assign val ue to 10

2. Compound assignment

var num = 10;
num = num+10;
//The above sentence can be replaced by the following one
num += 10;

8. Comma operator

1. Using comma operators, multiple operations can be performed in one statement.
2. Use comma operators for assignment, which always returns the last item of the expression.

//Declare and assign three variables at the same time
var num=1, num2 = 2 , num3 = 3;
//When assigning a value, the last item of the expression is always returned.
var n = (32,12,5,1,2);   //Finally n is assigned to 2

Last few sentences

This blog is more about data types and operators, you can see more times, you can also focus on the points of attention, key points, etc., has been marked out in particular. After reading the basic concepts here, it's over. Next, we will continue to update our notes. Please pay attention. Writing a blog is not easy. It takes a lot of thought. If you think it's good, pay attention to it and give me a compliment. Let me have the motivation to continue blogging. Haha.

This article is from Blog Garden: http://www.cnblogs.com/Ry-yuan/
Author: Ry
Welcome to reprint, reprint please indicate the source

Keywords: Javascript less encoding Java

Added by scotte on Sat, 25 May 2019 22:44:11 +0300