JS
Continue day0710
- Comparison operator
The comparison operator is used in logical statements to determine whether variables or values are equal.
Given x=5, the following table explains the comparison operator:
operator | describe | Example |
---|---|---|
== | Be equal to | x==8 is false |
=== | Equality (Value and Type) | x===5 is true; x===="5" is false |
Note: JavaScript is a scripting language. When the browser reads the code, it executes the script code compilation line by line.
-
Notes
- Multi-line comments begin with /* and end with */.
- One-line comments begin with //.
-
Variables (containers for storing information)
- js has only var variable
var x=2;//Declare variable x with an initial value of 2
var y=3;//Declare variable y with an initial value of 3
var z=x+y;//Operation and assignment
var num=6,num1=5,sum=num+num1;//Declare and assign values at the same time
</script>
function testVar(){
var num = 5;//Declare variable num with initial value of 5
var num1= 6;//Declare variable num1 with an initial value of 6
var sum=num+num1;
/*shangvar num=6,num1=5,sum=num+num1;//Declare and assign values at the same time*/
alert(sum);
}
</script>
<body>
<!--The result pops up when the button is clicked-->
<button onclick="testVar()">test</button>
</body>
javascript has dynamic types, which means that the same variable can act on different types
-
array
- Array objects are used to store a series of values in separate variable names.
- There are two ways to assign values to an array (you can add as many values as you want, just as you can define as many variables as you need).
- Array objects are used to store a series of values in separate variable names.
var mycars=new Array();//Declare an array, and the length of the array is not limited
mycars[0]="Saab";//Store a series of string types in an array
mycars[1]="Volvo";
mycars[2]="BMW";
var mycars=new Array(3);//Declare an array, and the length of the array is limited
mycars[0]="Saab";
mycars[1]="Volvo";
mycars[2]="BMW";
var mycars=new Array("Saab","Volvo","BMW");//Initialization values are given at declaration time
-
Access Array
- By specifying the array name and index number, you can access a particular element.
document.write(mycars[0]);//The result is Saab
/*It's fine too*/
for(var i =0;i<mycars.length;i++){
document.write(mycars[i]);//Output all values
}
- JavaScript objects
<script>
function person(){
var people ={name:"tom",age:20,sex:"male"};
var info="";
for(peo in people){
info+=people[peo]
}
document.getElementById("duixiang").innerHTML=info;
}
</script>
<body>
<div id ="duixiang"></div>
<button onclick="person()">test</button>
</body>
Test results:
</script>
function getNow()
{
var myDate = new Date();//Objects for time acquisition
var year = myDate.getFullYear();//year acquisition by object
var month = myDate.getMonth();//Obtaining months from objects
month = month + 1;
var date = myDate.getDate();//Obtaining Sky from Objects
var hour = myDate.getHours();//Obtain hours from objects
var minute = myDate.getMinutes();//Obtain minutes from objects
var second = myDate.getSeconds();//Obtain seconds from objects
var day = myDate.getDay();
var div = document.getElementById("time");
div.innerHTML=year+"/"+month+"/"+date+" "+hour+":"+minute+":"+second;
}
setInterval("getNow()" , 1000);//Every second and a new time
</script>
<body>
<div id ="time"></div>//It's only a matter of time before that happens.
</body>
<script>
function testdui(){
var user=new Object();//Create user objects
user.userid=1;//Declare the userid attribute
user.username="qinbo";//Declare the username attribute
user.userpwd="123456";//Declare the userpwd attribute
user.alertAll=function(){
document.getElementById("SHOW").innerHTML="user"+user.userid+"Password"+user.userpwd;
}
user.alertAll();
}
</script>
<body>
<div id ="SHOW"></div>
<button onclick="testdui()">test</button>
</body>
Operation results:
Variables declared inside JavaScript functions (using var) are local variables, so they can only be accessed inside functions. (The scope of the variable is local).
You can use local variables with the same name in different functions, because only functions that have declared the variable can recognize it.
As long as the function runs, the local variable is deleted.
- switch
<script>
function getDay(day){
var getday = document.getElementById("DAY");
switch(day){
case 1:
getday.innerHTML="Monday";
break;
case 2:
getday.innerHTML="Tuesday"
break;
case 3:
getday.innerHTML="Wednesday" ;
break;
case 4:
getday.innerHTML="Thursday"
break;
case 5:
getday.innerHTML="Friday"
break;
case 6:
getday.innerHTML="Saturday"
break;
case 7:
getday.innerHTML="Zhou Qi "
break;
}
}
</script>
<body>
<div id ="DAY"></div>
<button onclick="getDay(5)">test</button>
</body>
Operation results: