JavaScript is coming

1. Know what JavaScript is?
    JavaScript is an object- and event-driven client-side scripting language. It was originally designed to verify the correctness of HTML form input and originated from Netscape's LiveScript language.

2. Understand the development history of JavaScript

Able to handle complex calculations and interactive
important parts of the web
into a simple input validator to a powerful programming language
Simple and complex

3. Understand the components of JavaScript
Insert picture description here

4. Use JavaScript in HTML
You can use script tags to embed JavaScript scripts in the head or body.

Let's talk about grammar today

//Single-line comment
/**/Multi-line comment
End of statementsemicolonIf omitted, the parser will determine the end of the sentence.
It's best that we develop a good habit

Everything in ECMAScript (variables, function names, and operators) is case sensitive.

1. What isIdentifier?
The names of variables, functions, attributes, or function parameters.
2. The naming rules of identifiers:
(1), composed of letters, numbers, underscores (_) or dollar signs ($)
(2),Cannot start with a number
(3). Keywords, reserved words, etc. cannot be used as identifiers.

ECMAScript variables are loosely typed
: they can be used to store any type of data.
In other words, eachvariableJust one forPlaceholder for saved valueThat's it.

1. The variable declaration should use the var operator,
syntax: var variable name
2. Variable assignment: assignment
at the same time of declaration: var variable name = value,
first declaration and then assignment: variable name = value
-declare multiple variables at a time, separated by commas Open, such as:

var name_1;
name_1 = "ZhengQian";
var id,
	sex,
	age = 15,
name = "hurry";
console.log(age);  //在控制台中打印

Variables that omit var declaration are global variables
But we better not omit var to define global variables

JavaScript data type
Insert picture description here
How do you know what type it
withtypeof
Insert picture description here

console.log(typeof(name));  //在控制台中打印

The undefined type has only one value, namelySpecial undefined.
Explanation: That is, no definition.
Generally speaking, there is no need to explicitly set a variable to an undefined value.

nullValue represents aNull object pointer
If the defined variable is to be used to save the object in the future, it is best to initialize the change amount to null instead of other values.

console.log(undefined==null);  //在控制台中打印true

Explanation: The undefined value is derived from the null value, so the return result is true.

1. Master Number

NUmber stands for integer and floating-point number
NaN: Not a Number is a special value
NaN is also a number

console.log(typeof(18-"cbd"));//返回number
console.log(isNaN("16"));//返回false

Any operation involving NaN (such as NaN/10) will return NaN.
NaN is not equal to any value,Including NaN itself

2. Master the isNaN()
function: check whether it is a "non-numeric"
return value: boolean

n can be any type.
For the accepted value, this function willFirst try to convert to a number, And then check whether it is a non-numeric value.

3. Master the numerical conversion
Number()
parseInt()
parseFloat()
Pay attention to case
Number() can be used for any data type.
parseInt() and parseFloat() are specifically used to convert strings into numbers.
But these strings must start with a numeric value

console.log(Number("323"));//323
console.log(Number("323fsfd"));// NaN
console.log(parseInt("323fsfd"));// 323
console.log(parseInt("were323fsfd"));// NaN
console.log(parseInt("0xf"));// 15
console.log(parseInt("0xf",16));// 15

When it cannot be converted, NaN

parseInt(): will ignore the spaces in front of the string until the first
non-space character is found.
Explanation:
1. parseInt(): Convert an empty string and return NaN.
2. The parseInt() function provides the second parameter: the base number used during conversion
(that is, how many bases )

parseFloat : parse each character from the first character until an invalid
floating point character is encountered .
Note:
Except for the first decimal point, the second
difference between parseFloat() and parseInt() is that it always ignores leading zeros.

console.log(parseInt("12.35px"));// 12
console.log(parseFloat("12.35px"));//12.35
console.log(parseFloat("12.35.43px"));//12.35
console.log(parseFloat("0.1235px"));//0.1235

The String type is used to represent a character sequence composed of zero or more 16-bit Unicode characters, that is, a string. Strings can be represented by double quotation marks (") or single quotation marks (').

Syntax: str.toString()
Function: Convert str to a string
Return value: a copy of
str Parameter: str is the content to be converted, which can be numeric, boolean, object and string.
Note: You can also use it if you don’t know whether the value to be converted is null or undefinedString() function,itAbility to convert any type of value to a string

var ids=78965;
var idstr=ids.toString();
console.log (typeof idstr);
var isStudent = true; //则此时isStudent为Boolean型
console.log(typeof isStudent);
console.log(isStudent.toString());//则此时转换为'true'字符串
//一定要注意函数方法的大小写toString

Boolean ()
1, excluding0All numbers
except "" are converted to Boolean type are true 2, all characters except "" are converted to Boolean type are true
3.nullwithundefinedConvert to boolean to false

The conversion of spaces to boolean is true

So what is the significance of these variable types going around?
Generally speaking, it is said to pave the way for the latter, but not why?
In fact, type conversion can be judged for statements such as if(){}, etc.

js arithmetic operator

1. Grasp what is an
expression. The meaningful expressions that are connected with the same type of data (such as constants, variables, functions, etc.) by arithmetic symbols according to certain rules are called expressions.

2. Master the classification of javascript operators

  • Arithmetic operation marks
  • Logical operator
  • Argument Operator
  • Comparison operator
  • Ternary operator

3. Master arithmetic operators

+: add
-: subtract
*: multiply
/: divide
%: take the remainder

1. Increment
++a and a++ are both the operation of incrementing a.
Difference:
++a first returns the value of
a after increment, a++ first returns the original value of a, and then returns the value after increment
2. The same is true for decrement

var num1 = 10,
	num2 = "5";
	console.log(num1 - num2); //number NaN 隐式类型转换

var num1=10,
	num2=5,
	num3=num1++-num2;//++num1   num1=num1+1
console.log(num1) ;// 11  //11
console.log(num3) ;// 16   //5

Assignment operator:

=   +=  -=  *=   /=  %=

Note: Any string + any value will be concatenated, where the "+" sign is the concatenation

><>=<======!=!==
==∶相等,只比较值是否相等
===:相等,比较值的同时比较数据类型是否相等
!=∶不相等,比较值是否不相等
!==∶不相等,比较值的同时比较数据类型是否不相等
返回值:boolean型

var x=10,
	y="10",
	m=15//z=x==y;//值是否相等
	z=x===y;//全等
	n=x!==y;
console.log(n);

Ternary operator
Syntax:
condition? Execution code 1: Execution code 2
Description: It
can replace simple if statement,
if the condition is established, execute code 1, otherwise execute code 2.

Logical operator
&& ||! And or not

var num1=10,
	num2=20,
	num3=30,
	str="welcome" ,
	bool=true,
	n=null,
	m ;
console.log(numl<num2 && num2<num3);//true
console.log(num1<num2 && num2==num3);//条件都为true才返回true
console.log(numl<num2 && num2);//20
console.log(0 && num2);//0
console.log("" && num2);//""

&&(As long as one condition is not established, return false)
Description: InOne of the operands is not a booleanIn the case of, the logical AND operation does not necessarily return a value. At this time, it follows the following rules:
1. If the implicit type conversion of the first operand is true, the second operand is returned.
2. If the implicit type conversion of the first operand is false, the first operand is returned.

Ⅱ OR (return true as long as one condition is established)
Description: In the case that an operand is not a Boolean value, the logical OR operation does not necessarily return a value. At this time, it follows the following rules:
1. If the first operand is hidden After the type conversion is true, the first operand is returned.
2. If the implicit type conversion of the first operand is false, the second operand is returned.
3. If the two operands are null, return null.
4. If the two operands are NaN, return NaN.
5. If the two operands are undefined, return undefined.

!Not
Description:
1. No matter what data type the operand is, the logical negation will return a Boolean value.
2. !! When two logical negation operators are used at the same time: the
first logical negation operation will return a Boolean based on whatever operand is Value The
second logical negation negates the Boolean value

console.log(!!"");//false
console.log(!!"blue");//true

NEXT:
JavaScript branch statement!

Guess you like

Origin blog.csdn.net/qq_44682019/article/details/108892952