Basic Introduction to JavaScript
1. js is a web-based scripting language.
What is the scripting language?
(1) scripting languages cannot be used independently. They are used in combination with HTML/jsp/php/asp.net.
(2) The scripting language also has its own variables, functions, and control statements (sequence, branch, and loop)
(3) The Script statement is actually an explanatory language (that is, the source code is directly executed during execution .)
(4) Java program, Java->. class-> jvm js-> browser (js engine for interpretation and execution .)
2. js execution on the client (browser)
3. Because Javascript is interpreted and executed by the browser, there is a problem here. Different types of browsers may have different support for js.
Js Development Tool Selection
1 notepad
2. eclipse (myeclipse)
Case 1:
Requirement: After the webpage is opened, hello, world is displayed.
<Script language = "javascript">
Window. alert ("hello ");
</Script>
(1) js location?
Js location is free
(2) js is required
<Script language = "javascript">
// Js Code.
</Script>
Note: If the <script> package is not used, the browser regards it as plain text.
(3) Multiple <script> fragments can appear in an html file (jsp/php/asp), and the browser will execute them in sequence.
Case 2:
Improve the previous program into a simple addition operation program
<Script language = "javascript">
// Definition of variables in js (variables in js are represented by var regardless of the actual type)
Var num1 = 456;
Var num2 = 89;
Var result = num1 + num2;
Window. alert ('result is '+ result );
</Script>
How is the Js variable type determined?
(1) js is a weak data type language
That is, when defining variables, they are represented by var in a uniform manner, and even the keyword var can be removed.
(2) the data type of variables in js is determined by the js engine.
Var name = "feipeng"; // name is a string
Var kk = 2; // kk is a number
Var yy // yy is undefined
Name = 234; // The name is automatically converted to a number.
Js naming rules (variable functions)
(1) Names can be given using uppercase/lowercase letters, numbers, underscores, and dollar signs.
(2) It cannot start with a number.
(3) JavaScript keywords/reserved words cannot be used
(4) case sensitive
(5) single-row comment // multi-row comment /**/
Js Data Type
(1) Basic Data Types
Divided:
1, Value
For example, var a = 89; // a is an integer.
Var B = 35.6; // B is a decimal number.
Special Value
NaN (not a number)
Var a = "abc ";
Window. alert (parseInt ());
Infinity)
Window. alert (6/0 );
* Two functions can be used to determine NaN and infinity.
Window. alert (isNaN ("abc"); // return true
2, string
Example:
Var a = "abcd ";
Var B = 'abcdd ';
Var c = "adlaj \" jljkj"
Window. alert (c );
3. Boolean
Example:
Var a = true;
Var B = false;
You can see the specific data type through typeof?
Example:
<Script language = "javascript">
Var v1 = "abc ";
Var v2 = 890;
Window. alert ("v1 is" + typeof v1 );
Window. alert ("v2 is" + typeof v2 );
V1 = 89;
Window. alert ("v1 is" + typeof v1 );
</Script>
(2) composite type
Divided:
1, array
2. Object
(3) special types
1, null
Var a = null;
2, undefine
Window. alert (tt); // error, undefined
Var aa; // no value is given
Window. alert (aa); // The undefine dialog box is displayed;
Conversion of js Data Types
1. automatic conversion
Var a = 123; // a is a value.
A = "hello"; // The type of a is string
2. Forced conversion
For example:
V ar a = "12345 ";
A = parseInt (a); // use the system function to force the conversion.
Var B = 90; // B is number
B = B + "; // B is string
Js Operators
+ ,-,*,/
% (Modulo is used to calculate the remainder of two numbers. It is usually used to determine whether two numbers are divisible, mainly used as integers)
Example:
Var a = 90;
Var B = 8;
If (a % B = 0 ){
Window. alert ("divisible ");
} Else {
Window. alert ("cannot be divisible ");
}
++ Operator
Example:
Var a = 56;
Var B = ++ a; // B = ++ a; <=> [a = a + 1; B = a;] B = a ++; <=> [B = a; a = a + 1;]
Window. alert (B );
Window. alert ();
-- Operator
Var a = 56;
Var B = -- a; // B = -- a; <=> [a = A-1; B = a;] B = --; <=> [B = a; a = A-1;]
Window. alert (B );
Window. alert ();
+ = Left plus
-= Left Subtraction
/= Left Division
% = Left modulo
Case:
Var a = 56; var B = 90;
A-= 34;
B % =;
Window. alert (B );
Window. alert ();
This section describes window. prompt, and document. writeln ();
Example:
Var val = window. prompt ("Enter the value :");
Var val2 = window. prompt ("enter another value :");
Document. writeln ("your input is:" + (parseFloat (val) + parseFloat (val2 )));
Relational operators
1, = equal to 2,> greater than 3, <小于< p>
4,> = greater than or equal to 5, <= less than or equal
6 ,! = Not equal
Case:
Var num1 = window. prompt ("Enter the first number ");
Var num2 = window. prompt ("enter the second number ");
Num1 = parseFloat (num1 );
Num2 = parseFloat (num2 );
If (num1> num2 ){
Window. alert ("num1> num2 ");
} Else if (num1
Window. alert ("num1
} Else {
Window. alert ("num1 = num2 ");
}
Javascript control statements
(1) Sequential Control
For programming, if you do not control the process, it is sequential execution.
(2) Branch Control
Single Branch
Basic syntax
If (conditional expression ){
// Execute the statement
}
Dual Branch
Basic syntax
If (conditional expression ){
} Else {
}
Write a program that can input the age of a person, if the comrade
If the age is more than 18 years old, the output "you are older than 18 years old, to yourself
!". Otherwise, output "your age is not big.
After you ".
Code:
Var age = 20;
If (age> 18 ){
Window. alert ("greater than 18 ");
} Else {
Window. alert ("less than 18 ");
}
Multi-branch
Basic Syntax:
If (conditional expression 1 ){
// Execute
} Else if (condition expression 2 ){
// Execute
} Else ...{
// You can have multiple else if
} Else {
// No.
}
Note: Once a qualified entry is found, the multi-branch is directly ended after execution.
Switch
Basic Syntax:
Switch (expression ){
Case constant 1:
// Execute the statement
Break;
Case constant 2:
// Execute the statement
Break;
}
...
The switch statement data type of js allows any types supported by js (outside the array and object)
The data types after case can be arbitrary (except for arrays and objects)
Js Functions
(1) Why functions are required?
(2) Basic concepts of functions
A set of code (statements, instructions) for completing a function
(3) basic syntax
Function Name (parameter list ){
// Code
Return Value; // optional.
}
(4) case studies