Since I have learned Java before, I will skip the same features and usage of Javascript and Java here.
1, Javascript output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js output</title>
</head>
<body>
<p id="one"></p>
<script type="text/javascript">
/**
window.alert() Warning box
document.write() Write to HTML document
innerHTML Write to HTML element
console.log() Console written to browser
**/
alert('helloworld');//A warning box will pop up. The display content is helloworld. Click OK to execute the following code
//Write content directly to the entire HTML document
document.write('helloworld');
//Change p element content
document.getElementById('one').innerHTML = "this is p";
//Output content in the console
console.log(123)
</script>
</body>
</html>
2, Javascript data types
Note that var can represent any of the above data types.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js data type</title>
</head>
<body>
<script type="text/javascript">
/**
* String (String)
* Number
* Boolean
* Array
* Object (Object)
* Null
* Undefined
*/
// var can declare various types
var x; // x is undefined
var y = 5; // y is the number
var z = "John"; // z is a string
// character string
var carname="Volvo XC60";
var carname='Volvo XC60';
// number
var x1=34.00; //Use decimal point to write
var x2=34; //Write without decimal point
// Boer
var x=true;
var y=false;
// array
var cars=new Array();
cars[0]="Saab";
cars[1]="Volvo";
var cars=new Array("Saab","Volvo","BMW");
var cars=["Saab","Volvo","BMW"];
// JavaScript object key value pair
var person={
firstname:"John",
lastname:"Doe",
id:5566
};
console.log(person.firstname)
console.log(person["firstname"])
// Undefined and Null
// Undefined indicates that the variable does not contain a value.
// You can empty a variable by setting its value to null
var persons;
var car="Volvo";
car=null
</script>
</body>
</html>
3, Javascript functions
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js function</title>
</head>
<body>
<script>
/**
• A function is an event driven or reusable block of code that executes when it is called
• function a( Argument) {} declaration will be preceded
• var a = function( Parameter) {} anonymous function
• return in function
*/
//Declaration and call of nonparametric function a
function a()
{
console.log(1111);
console.log(2222);
console.log(3333);
console.log(4444);
console.log(5555);
console.log(6666);
}
a();
//Declaration and call of function b with parameters
function b(cs1,cs2)
{
console.log(cs1+cs2);
}
b("Hello,","world");
//Declaration and call of anonymous function
var c=function (cs1,cs2,cs3)
{
console.log(cs1+""+cs2+""+cs3);
};
c(1,2,3);
//return of function
function d()
{
return "This is return function";
}
var d1=d();
console.log(d1);
</script>
</body>
</html>
4, Javascript comparison and logical operators
It is basically the same as in Java. The unique comparison characters of JS are = = = and! = =, the former is strictly equal (i.e. both values and data types are equal), and the latter is completely unequal. For example, VAR C = 5; VAR B = '5'; if = = is used, the result is true; if = = = is used, the result is false, because one data type is number and the other is String
5, switch in Javascript
Unlike Java, the switch in JS can match the string, which needs to be noted. For example, in the following code, the number input from prompt is of string type, so it is case "1" to case "3" in switch.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var number=prompt("Please enter the business number you want to query");
switch (number)
{
case "1":
{
document.write("Account balance is:. . . . . . ")
break;
}
case "2":
{
document.write("The transfer balance is:. . . . . . ")
break;
}
case "3":
{
document.write("The credit limit is:. . . . . . ")
break;
}
default:console.log("Exit the banking system");
}
</script>
</body>
</html>
6, Loop in Javascript
The only difference from Java is that there is a special loop for/in in JS, which can loop through objects
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js in for Circulation and while loop</title>
</head>
<body>
<script type="text/javascript">
/**
*
* for - Loop code block a certain number of times
for/in - Loop through objects
while - Loop the specified code block when the specified condition is true
*/
for( var i = 0; i < 10 ; i++ ){
console.log('i:'+ i);
}
//Declare and assign values to objects
var person = {
name : 'json',
age : 25,
sg: 170
}
//Loop out the value of the key value pair in the object person
for( key in person ){
console.log(person[key])
}
var count = 1;
while( count < 10 ){
count++ ;
console.log('implement while loop');
}
</script>
</body>
</html>