JS Basic syntax

Source: Internet
Author: User
Tags arithmetic operators

JS's comments and semicolons
Single-line Comment
/**/Multi-line Comment Ctrl +shift +/
The statement ends with a semicolon, and if omitted, the end of the statement is determined by the parser
JS syntax

1. Variables, function names, operators are case-sensitive
2. Identifiers
(1) What is an identifier
The name of a variable, function, property, or parameter of a function
(2) naming rules for identifiers
1> consist of alphanumeric underscores or $
2> cannot start with a number
3> cannot use keywords, reserved words, etc. as identifiers
3. Variables
The 1.ECMAScript variable is loosely shaped.
Loose type: Can be used to save any type of data
In other words, each variable is just one to save the worthy placeholder.
2. Variable declaration
The declaration of a variable is to use the var operator
Syntax: VAR variable name
Note: Variables declared by omitting Var are global variables and are not recommended for use
3. Assigning values to variables
Method one: first declaration and then assign the value
var xx;
xx= ' Hello ';
Method Two: Simultaneous assignment of declarations
var age=18;
Declare multiple variables at once
var a=12,b= ' Hello ', C;
4. Data type
Simple data type (base data type)
Undefined use only one value---special Undefined
A variable type that is not assigned is undefied
Null pointer object
If a defined variable is ready to be used for saving an object in the future, it is best to initially change it to null
Type conversions
0, empty string, null, undefined converted to Boolean is false the other is true
Integers and floating-point numbers
NaN not a number is a special value
Any operation that involves Nan (NaN-10) returns Nan
Nan is not equal to any value, including Nan itself
IsNaN (n)-----function detects whether n is a non-numeric value, returns True if the value is returned flase
After receiving the parameter, try to convert the parameter to a value, if you can turn and then return the result such as ' 16 ' is also the value
Used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string that can be represented by double or single quotation marks
Convert other types to string functions
ToString () function
Syntax: abc.tostring ()
Return value: a copy of ABC
Parameters: ABC is the content to be converted, can be a numeric value or a Boolean or an object or a string
String () function
Forced conversions, ability to convert any type of value into a string

ECMAScript6 added the symbol type
Complex data types
typeof---checking variable types
typeof variable or typeof (variable)
return value String type
Console.log (typeof age)//print in console
Numerical conversions (3 functions can convert non-numeric values to numeric types)
Number () forces the value to be converted to a data type, and returns Nan when the string is not transferred
parseint () is specifically used to convert a string into a numeric value
1. The parameters of the operation must be the beginning of the number to be extracted, such as 28px can be ss28 extracted to return Nan
2.PARSEINT () receives two parameters, one is the converted parameter one is the cardinality used in the conversion (how many binary)
var topval= ' 28px ';
Topval=parseint (Topval)
Console.log (Topval)//return value is 28
Parsefloat () is specifically used to convert a string into a numeric value
Parses each character starting from the first character until an invalid floating-point value is encountered
In addition to the first decimal point, the second difference from parseint () is that it always ignores the leading 0
5. Operators
Arithmetic operators
+-*/% ++a (returns the value after increment) a++ (return the original value first, then increment)
Other operators
logical operators
Logic and &&
Description: When there is an operand that is not a Boolean value, the logic and operation do not necessarily return a value, at which point the following rules are observed:
1. If the 1th operand is true after an implicit type conversion, the 2nd operand is returned
2. If the 1th operand is false after an implicit type conversion, the 1th operand is returned
3. If one of the operands is None or Nan or undefined directly returns none or Nan or undefined
Logical OR | |
Description: When there is an operand that is not a Boolean value, the logic or operation does not necessarily return a Boolean value, following the rules
1. Returns the first operand if the first operand of a hermit type is converted to true
2. If the first operand of the hermit type is converted to false, it is judged one time, until it encounters a true return
3. Returns none or Nan or undefined if the operand is none or Nan or undefined
Logical Non! -----always return a Boolean type
1. Logic does not return a Boolean value regardless of the data type of the operand
2.!! Simultaneous use of two logical non-operating characters
The first returns a Boolean value based on the operand
The second negation of the Boolean value
Assignment operator
Comparison operators
= = equals, only compare values are equal
= = = Equal, comparison is worth comparing data types equally
! =: Not equal, the comparison value is not equal
!==: Not equal, it is worth comparing data types for unequal
Return Value: Type Boolean
Ternary operator
Conditions? Execute code 1: Execute code 2
Can replace a simple if statement
If the condition is true, execute code 1, otherwise execute code 2

JS Basic syntax

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.