JS Basics-Data types-operators and expressions-variables and constants

Source: Internet
Author: User
Tags arithmetic operators bitwise

Basic syntax for 1.js
2.js Commissioning
1.F12 bring up the console to see the prompt error and its location.
2. Error only affects the current code block, does not affect other blocks of code, subsequent code blocks continue to execute.
3. Syntax specification
1.js statement: The smallest unit that can be executed
must take; End
Strictly case-sensitive
Ex:console.log (); That's right
Console.log (); Error
All symbols must be English punctuation
2.js comments
: Single-line comment
/**/: Multiline Comment
4. Variables and Constants
1. Variables
1. Declaring variables
var variable name;
Ex
var uname;
2. Assigning a value to a variable
Variable name = value;
3. Assigning values directly when declaring variables
var variable name = value;
Attention:
1. Allow multiple variables to be declared in a single statement, separated by commas.
Ex
var user_name= "hanmm", user_age=18;
2. If you declare a variable but are not assigned a value, the value defaults to undefined.
3. var can be omitted when declaring a variable, but not recommended.
2. Naming conventions for variable names
1. Keywords and reserved keywords are not allowed
var const if else for while int string ...
2. Allow to include letters, numbers, underscores, $
3. Cannot start with a number
4. Best known
Ex
var A;
var uname;
5. Try to use the "small Hump naming" method
var uname;
var username;//small Hump

var username;//big Hump
var user_name;//underline

3. Use of variables
1. Declaring variable unassigned value is undefined by default
2. Use undeclared variables to error.
3. Value Operation-get
As long as the variable does not appear on the left of =, it is the value operation.
4. Assignment Operation-set
Variable names appear on the left side of = and are assigned operations.
Practice:
Use three variables to save notebook information:
Notebook name
Notebook Price
Notebook Inventory
and print the notebook information in the console: the output format is as follows:
Ex
Notebook Name: ThinkPad E460
4. Common use
1. What is a constant?
In a program, data that is not allowed to be modified once it is declared is a constant.
2. Syntax
Const constant NAME = value;
Constant names are used in programs, usually in uppercase.
Ex
Const pi=3.14;
Practice:
1. Create a constant pi, assign a value of 3.14, and print it in the console,
Then change the value of the declared constant pi to 31.4, try to print the output, and observe the result.
5. Data type
1. The role of data types
Specifies the amount of space the data occupies in memory.
40:4 bytes or 32 bits
40.1
Bit: Bits
8bit=1byte bytes
1024byte=1kb
1024kb=1mb
1024mb=1g
1024g=1t
2. Detailed Data types
1. Data type classification
1. Primitive type (basic type)
1.Number type
Number Type
Function: can be used to represent a 32-bit integer, or a 64-bit floating-point number (decimal).
Integer:
1. Decimal
Made up of 0-9 numbers, stitch ten into a
var age=25;
2. Eight binary
Made up of 0-7 digits, stitch eight into a
Start with 0 in octal
3.16 Binary
Made up of 0-9 and a-f, Stitch 16 into a
A:10
B:11
C:12
D:13
E:14
F:15
Start with ox in hex
Floating point number (decimal):
Decimal point counting method: 12.34
Exponential counting Method: 3.4e3 (3.4 times 10 of 3 square)
2.String type
String type
Function: Represents a series of text character data, such as: Name, gender, address, etc.
Each character has a unique encoding in the computer that represents the character, which is called a Unicode code.
To find the Unicode code for a character:
"One". charCodeAt ();//Default Decimal
"One". charCodeAt (). toString (16);//Hex

674e-->unicode Convert to Kanji?
Escape character: \u
Other escape characters:
\ n: Line break
\ t: Tab (Indent)
\":"
\‘:‘
\\:\
Strings are used in the process and need to be enclosed in "or".
3.Boolean type
Effect: The result of true or false expression in a program
Value: TRUE or False
Ex
var isbig=true;
When participating in a numeric operation, true can be used as a 1 operation, and false can be used as a 0 operation.
Ex
var result=25+true;//results: 26

4.Undefined type
Function: Indicates that the data used does not exist
The undefined type has only one value, that is, undefined, when the declared variable is not assigned a value, the default value of the variable is undefined.
Ex
var num;
Console.log (num);//Results undefined

5.Null type
Null is used to represent an object that does not yet exist.
A null type has only one value, which is null, and is usually null if the function or method returns an object and the object is not found.
Ex
document.getElementById ("uname");

2. Reference types
2. Data type Conversion
1. Implicit (automatic) conversion
Different types of data are automatically converted during the calculation.
1. Numbers + strings: converting numbers to strings
var num=15;
var str= "Hello";
var Result=num+str;//15hello
2. Number + Boolean: Convert Boolean to Digital
var num=15;
var issun=true;
var result=num+issun;//results 16
3. String + Boolean: Converts a Boolean type to a string
var str= "Hello";
var isbig=true;--"true"
var result=str+isbig;//results Hellotrue
4. Boolean + Boolean: Converts a Boolean type to a number
var isbig=true;
var Issun=false;
var result=isbig+issun;//results 1
Problem:
var num1=15;
var num2=17;
var str1= "Hello";
1.str1+num1+num2
Results: Hello1517
2.num1+num2+str1
Results: 32Hello
Allows the use of the typeof () function to check the data type of the variable.
2. Cast-Convert function
1.toString ()
Convert any type of data to a string
Syntax: var result= variable. toString ();
Ex
var num=15;
var result=num.tostring ();
Console.log (typeof (Result));//string
2.parseInt ()
Convert any type of data to an integer
If the conversion is unsuccessful, the result is Nan (not a number)
Syntax: var result=parseint (data);
Ex
var num= "123ABC"; Results 123
var num1= "abc123";//Result Nan
3.parseFloat ()
Convert any type of data to decimals
If the conversion is unsuccessful, the result is Nan.
Syntax: var result=parsefloat (data);
4.Number ()
Convert any type of data to number
Note: If an illegal character is included, a nan is returned
Syntax: var result=number (data);
Practice:
1. Pop up an input prompt, enter the amount, and save in the variable money
Ex
var money=prompt ("Please enter Amount:");
2. Add money+10, re-print output plus 10 results
3. Comment out the 2nd step and use typeof () to check the type of money.
4. Convert money to a number, and then +10 to print out.
6. Operators and expressions
1. Expression: An expression consisting of an operator and an operand
Any expression must have a result.
Ex
15+17
var x=y=10;
17<15
2. Operators
1. Arithmetic operators
+,-,*,/,%,++,--

5%2=1 5 modulo 2 remainder is 1
+ +: Self-increment, do only +1 operation
+ + in front, first self-increment, then calculate
+ + in the rear, the first operation, then self-increment
--: Self-subtraction, only do-1 operation
--Before, the first self-subtraction, and then the operation
--In the latter, the first operation, then the self-reduction
Ex
var num=5;
Console.log (num++);//5
Console.log (++num);//7
Practice:
var num=5;
5 (6) 6 6 (7)
var result=num + ++num + num++ + ++num + num++ +num; Results: 42
(8) 8 8 (9) 9
2. Relational operators (comparison operators)
>,<,>=,<=,==,!=,===,!==
The result of the operation is: Boolean type (True,false)
Problem:
1.5> "10" Result: false
At both ends of the relational operator, if one is number, the other is implicitly converted to number and then compared.
2. "5" > "10" Result: true
The relationship operator ends with a string that compares the Unicode code of each character to the result of a two-character comparison that is not equal to the Unicode code.
"Zhang San Feng" > "Zhang Mowgli" result: false
"Three". charCodeAt ();
"None". charCodeAt ();
3. "3a" >10 Result: false
Number ("3a")--->nan
NaN
Attention:
Nan The result is false when comparing operations with any one of the data.

Nan with any data do! =, the result is always true.

Nan with any data = =, the result is always false.
IsNaN () function:
Function: Determines whether the specified data is non-numeric, if it is not a number, the return value is true, the return value is False if it is a number.

3. Logical operator
logical operator: &&,| |,!
!: to reverse
&&: And, when the associated two conditions are true, the result of the entire expression is true.
| | : Or, an association of two conditions, as long as one is true, the result of the entire expression is true.

Short-circuit logic and:&& make a conditional decision
when the first condition is false, the result of the whole expression is false, and the second condition is no longer judged or executed.
When the first condition is true, the second condition continues to be judged or executed.
Short-circuit logic or: | |
When the first condition is true, subsequent expressions are no longer executed, and the overall result is true.
When the first condition is false, the second condition or operation continues.
4. Bitwise operator
<<,>>,&,|,^
Shift right is to make the number smaller, the left is to make the number larger
bitwise AND:& to determine parity
any number and 1 to do the bitwise AND, the result is 1, then the odd, the result is 0, is an even number.
Var num=323;
var result=num & 1;
Console.log (Result);
Bitwise OR: | Rounding decimal
Places any decimal number with 0 as the bitwise OR, and the result takes the integer part.
Var num=123.45;
var result=num | 0;
Console.log (Result);
Bitwise XOR: ^ for exchange of two numbers
Var a=3;
var b=5;
A=a^b;
B=b^a;
A=a^b;
Console.log (a);
Console.log (b); the number of bits, the difference is 1, the same is 0.

Exercise:
Declare two variables, and save two integers respectively. The
uses XOR or implements the transposition of the digits. The
output is as follows:
Console.log ("Before transposition A:" +a);

Console.log ("post-transposition A:" +a);


JS Basics-Data types-operators and expressions-variables and constants

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.