JavaScript Basics Article

Source: Internet
Author: User
Tags string methods

All data types:
Basic data type:
Undefined
Number
Boolean
Null
String
Complex data types:
Object

①undefined:
>>> declared but uninitialized variable: typeof-undefined
Aler-Undefined
Non-declared variable: typeof-undefined
Alert-Error

②null:
>>>typeof a variable with a null value returns an object
>>>undefined derived from Null,undefined==null returns true;


③boolean:
>>>boolean () function:
1.boolean:ture false
2.String: Non-empty string is true, empty string is False
3.number:0 is false, all non-0 are true
4.null/nan/undefined: All is False
5.object:null is false, all others are true


④number:
>>>nan: Not equal to any value, including NaN
>>>isnan ():
1. Pure numeric string: false
2. Floating-point numeric string: false
3. String containing other characters such as letters: true
4. Empty string: false
5.bool type, False
6.Null Type: false
7.undefined:true


>>>number (): Converting other types to numeric types
[String type to value]
>>> The string is a pure numeric string, it is converted to the corresponding value
>>> When the string is an empty string, it is converted to 0 "" and 0
>>> cannot convert "111a" when string contains other non-numeric characters->nan
>>> Floating-point numeric string to corresponding floating-point number

[Boolean to numeric]
true->1 false->0

[null/undefined to value]
null->0 undefined variable->nan

[Object type to value]
the valueof function is called first to determine if the function has a return value, and then to convert the return value based on the above situation


>>>parseint (): Convert other types to floating-point value types
[Convert a string to a numeric type]
principle: 1. Ignores whitespace to find the first non-whitespace character, if not a number or minus sign returns Nan, so the empty string is Nan (number () returns 0)
2. Until resolved to the end, or encountered a non-numeric character (the decimal point is a non-numeric character)
3. Returns all characters that were parsed successfully.
>>> empty string, cannot go: result is nan!!!
>>> Pure numeric string can be turned "123"->123
>>> Floating-point numeric string: "123.5"->123
>>> A string containing other characters that will intercept the number part before the first non-numeric character
"123a456"->123
"a123a456"->nan
>>>parseint () can only be converted to string and number types, boolean/null/undefined are Nan
>>>number types of floating-point type to integer type, integer invariant


>>>parsefloat ():
principle: 1. Ignore spaces to find the first non-whitespace character, If it is not a number or minus sign returns Nan, the empty string for Nan (number () returns 0)
2. Until the end is resolved, or a non-numeric character is encountered (the decimal point is non-numeric character)
3. Returns all characters that were parsed successfully.
[convert string to numeric]
>>> Use the same parseint (), but when converting a decimal string, keep the decimal point.
[parameter passed in is number]
Span style= "font-family:"microsoft yahei"; Font-size:16px "> >>>number type, floating point invariant, integral type return integral type.


⑤string:
[Converting other types to string methods]
①number,bool,object Call using the ToString () method; num.tostring ();
②null,undefined does not have the ToString () method, using the string () function: string (value);
③ let an empty string add this variable, "' +value

⑥object:



Operator:
① self-increment + +, auto minus--operator:
>>> for non-numeric operands, the type conversion is preceded by the number () function, which performs the +1-1 operation
1. String: Can be turned, converted to a number, then +1, the string variable becomes a numeric variable.
Non-rotating, sets the value of the variable to Nan, and the string variable into a numeric variable.
2. Boolean value: True-1 then add minus 1
False--0 then add minus 1.
3. Numeric: Floating-point and integral type direct plus minus 1


② unary plus and minus operators
>>> Use the above two operators before non-numeric values, and the rule is the same as number ().
>>> unary minus operator will change the value to negative


③ Boolean operator: A total of three non-! with && or | |
>>> Logical non: it applies to any data type, and the return value is a Boolean type.
It first converts the operand to a Boolean value and then negation.
The >>> rules are as follows:
1. False for Object
2. String: null string-True
non-empty string-false
3. Value 0-True
not 0 value-false
4.null/nan/undefined-True
>>> uses two logical non-operators, which is equivalent to using a Boolean ()


>>> Logic: It can be applied to any type of operand.
when there is an operand that is not a Boolean type, the return value is not necessarily a Boolean value.
The >>> rules are as follows:
1. If the first operand is an object, returns the second operand.
2. If the second operand is an object, the object is returned only if the first operand evaluates to true.
3. If the two operands are objects, the second operand is returned.
4. If one of the operands is null/nan/undefined, return null/nan/undefined.
>>> Logic and operation is short-circuit operation:
whenever an operand with a result of false is encountered, no matter what comes next, it is not evaluated and returns false directly.


>>> logic or: it can be applied to any type of operand.
when there is an operand that is not a Boolean type, the return value is not necessarily a Boolean value.
The >>> rules are as follows:
1. If the first operand is an object, the first operand is returned.
2. If the first operand evaluates to False, returns the second operand.
3. Two operands are objects that return the first operand.
4. If two operands are null/nan/undefined, return to null/nan/undefined.
>>> Logic and operation is short-circuit operation:
whenever an operand with a result of true is encountered, no matter what comes next, it is not evaluated and returns true directly.


typeof operator:
① Not defined->undefined
② string->string
③true/false->boolean
④ numerical->number
⑤ function->function
⑥ Object/null->object
>>> less null than data type, more function
>>> in ECMAScript, a function is an object, not a data type.
However, functions have some special properties, so it is necessary to differentiate functions and objects through the typeof operator.
>>> typeof operation results for declared but uninitialized and undeclared variables are undefined
but alert uninitialized returns undefined
alert not defined will cause an error.

JavaScript Basics Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.