Related explanations about JavaScript basics
Variable and data type issues in JavaScript:
About variable definitions in JavaScript:
var variable name = value; In js, var can be used to define variables of any data type and can be omitted, but it is not recommended; Because JavaScript is a weakly typed language, and Java is a strongly typed language;
3> If a variable has no value (undefined), the undefined variable cannot be used directly; They are all number type (lowercase);
3>
##
<script type="text/javascript"> //定义变量 var a = 10 ; //重复定义 //var a ; var b = 12.34 ; var c = 'a' ; var d = "hello" ; var e = true ; var f = new Object() ; //输出变量 document.write("a的值是:"+a+",a的数据类型是:"+typeof(a)+"<br/>") ; document.write("b的值是:"+b+",b的数据类型是:"+typeof(b)+"<br/>") ; document.write("c的值是:"+c+",c的数据类型是:"+typeof(c)+"<br/>") ; document.write("d的值是:"+d+",d的数据类型是:"+typeof(d)+"<br/>") ; document.write("e的值是:"+e+",e的数据类型是:"+typeof(e)+"<br/>") ; document.write("f的值是:"+f+",f的数据类型是:"+typeof(f)+"<br/>") ; </script>
String----Integer of type number
parseInt (variable);
STIRNG ---- Number Type of Number Parsefloat (variable);
Example:
Operators in JavaScript:
Arithmetic operators: (addition), - (subtraction), * (multiplication), / (division); Comparison operators: > ;=, <=, <, > . . . ; Logical operators: && (logical double AND), || (logical double OR), ! (logical NOT);
Note:
In JavaScript, true represents 1 and false represents 0;
Example:
<script type="text/javascript"> //定义一个变量 var a = "10" ; //string :里面的值10 document.write("转换前a的数据类型是:"+typeof(a)+"<br/>") ; //转换 a = parseInt(a) ; document.write("转换后a的数据类型是:"+typeof(a)+",值是:"+a) ; document.write("<hr/>") ; var b = "3.14" ; document.write("转换前b的数据类型是:"+typeof(b)+"<br/>"); //转换 b = parseFloat(b) ; document.write("转换后b的数据类型是:"+typeof(b)+",值是:"+b+"<br/") ; </script>
Process statement:
if statement:
if(表达式){
语句1;
} else {
语句2;
}
表达式的类型:
1> 如果是numbler类型,(整数类型,小数),0表示条件不成立;
2> 如果是string类型,要么字符要么字符串,必须有数据,如果是空串,条件不成立;
3> 如果是boolean类型,true成立,false不成立;
4> 如果是object类型,必须有对象,才成立,为null不成立;
switch语句:
格式:
和Java相同;
和Java的区别:
1> js中的case后面也可以跟常量,还可以跟变量,还可以跟表达式;
2> java中case后面只能跟常量;
举例:
<script type="text/javascript">
//case后面跟表达式
var age = 25 ;
switch(true){
case age >=18 :
document.write("你是成年人...") ;
break;
case age<18 :
document.write("你是未成年人呢...") ;
break ;
}
document.write("<hr/>");
var i = "b" ; //和Java语言一样,case后面可以常量,
var b = "b" ;
switch(i){
case "a" :
document.write("a") ;
break ;
case b :
document.write("b") ;
break ;
case "c" :
document.write("c") ;
break ;
case "d":
document.write("d") ;
break ;
}
</script>
Copy after loginJavaScript中的循环语句:
如:do-while,for,while 循环语句与java中的循环语法结构相同;
练习:
<script type="text/javascript">
//显示"*"形,第一行有一个...
for(var i = 0 ; i<5; i++){//外层循环控制行数
for(var j= 0 ; j<=i; j ++){//内层循环控制列数
document.write("* ") ; // 空格 转义字符
}
document.write("<br/>") ;
}
document.write("<hr/>") ;
//table标签: 使用js里面嵌套表格标签
//document.write("<table align='center'>") ;
for(var i = 1 ; i <=9 ; i++){
//
for(var j = 1 ; j<=i ;j++){
document.write(i+"*"+j+"="+(i*j)+" ") ;
}
document.write("<br/>") ;
}
</script>
Copy after login
wIth语句:
格式:
with(document){
语句;
}
特点:将document对象当做witch语句参数,然后直接使用它里面的write();
举例:
<script type="text/javascript">
//with语句:格式 with(document){....}
//将document对象当做witch语句参数,然后直接使用它里面的write() ;
//with语句
with(document){
for(var i = 0 ; i < 5 ; i ++){
for(var j = 0 ; j <=i ; j++){
write("* ");
}
write("<br/>") ;
}
write("<hr/>") ;
for(var i = 1 ; i <=9 ; i++){
for(var j = 1 ; j <= i ;j++){
write(i+"*"+j+"="+(i*j)+" ") ;
}
write("<br/>") ;
}
}
</script>
Copy after loginfor-in语句:
作用:针对数组或者对象进行遍历;
格式:
for(var 遍历的变量名 in 数组名称/对象名称){
输出变量名;
}
举例:
<script type="text/javascript">
var arr = [10,20,30,40] ; //类似于Java中的静态初始化
for(var i =0 ; i < arr.length; i++){
document.write(arr[i] +" ") ;
}
document.write("<hr/>");
//使用for-in语句
for(var i in arr){
document.write(arr[i] +" ") ;
}
Copy after login</script>
Copy after login函数:
格式:
function 函数名称(形式参数列表){
语句;
}
函数调用:
函数名称(实际参数列表);
函数中注意的问题:
1> 函数的形式参数不能有var关键字定义,否则报错;
2> 在js中函数是可以有return语句的,直接return,但是没有返回值;
3> 在js中,函数是不存在的重载概念,后面定义的函数回覆盖掉前面定义的函数;
4> 当实际参数的数量小于形式参数的数量,就会出现一个值肯定是NaN;
如果实际参数大于形式参数,那么最终会计算前面的值,然后将后面的数据丢弃掉;
5> 在js的每一个函数中,里面存在默认的数组:arguments,它的作用就是将实际参数从左到右依次赋值给形式参数(从左到右);
举例:
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
4 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
3 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
4 weeks ago
By DDD
Roblox: Dead Rails - How To Complete Every Challenge
1 months ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
2 weeks ago
By DDD
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
How to implement an online speech recognition system using WebSocket and JavaScript
Dec 17, 2023 pm 02:54 PM
How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.
WebSocket and JavaScript: key technologies for implementing real-time monitoring systems
Dec 17, 2023 pm 05:30 PM
WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology
How to use JavaScript and WebSocket to implement a real-time online ordering system
Dec 17, 2023 pm 12:09 PM
Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order
How to implement an online reservation system using WebSocket and JavaScript
Dec 17, 2023 am 09:39 AM
How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.
JavaScript and WebSocket: Building an efficient real-time weather forecasting system
Dec 17, 2023 pm 05:13 PM
JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We
Simple JavaScript Tutorial: How to Get HTTP Status Code
Jan 05, 2024 pm 06:08 PM
JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest
How to use insertBefore in javascript
Nov 24, 2023 am 11:56 AM
Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
JavaScript and WebSocket: Building an efficient real-time image processing system
Dec 17, 2023 am 08:41 AM
JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
See all articles
语句1;
} else {
语句2;
}
表达式的类型:
1> 如果是numbler类型,(整数类型,小数),0表示条件不成立;
2> 如果是string类型,要么字符要么字符串,必须有数据,如果是空串,条件不成立;
3> 如果是boolean类型,true成立,false不成立;
4> 如果是object类型,必须有对象,才成立,为null不成立;
格式:
和Java相同;
和Java的区别:
1> js中的case后面也可以跟常量,还可以跟变量,还可以跟表达式;
2> java中case后面只能跟常量;
<script type="text/javascript"> //case后面跟表达式 var age = 25 ; switch(true){ case age >=18 : document.write("你是成年人...") ; break; case age<18 : document.write("你是未成年人呢...") ; break ; } document.write("<hr/>"); var i = "b" ; //和Java语言一样,case后面可以常量, var b = "b" ; switch(i){ case "a" : document.write("a") ; break ; case b : document.write("b") ; break ; case "c" : document.write("c") ; break ; case "d": document.write("d") ; break ; } </script>
<script type="text/javascript"> //显示"*"形,第一行有一个... for(var i = 0 ; i<5; i++){//外层循环控制行数 for(var j= 0 ; j<=i; j ++){//内层循环控制列数 document.write("* ") ; // 空格 转义字符 } document.write("<br/>") ; } document.write("<hr/>") ; //table标签: 使用js里面嵌套表格标签 //document.write("<table align='center'>") ; for(var i = 1 ; i <=9 ; i++){ // for(var j = 1 ; j<=i ;j++){ document.write(i+"*"+j+"="+(i*j)+" ") ; } document.write("<br/>") ; } </script>
with(document){
语句;
<script type="text/javascript"> //with语句:格式 with(document){....} //将document对象当做witch语句参数,然后直接使用它里面的write() ; //with语句 with(document){ for(var i = 0 ; i < 5 ; i ++){ for(var j = 0 ; j <=i ; j++){ write("* "); } write("<br/>") ; } write("<hr/>") ; for(var i = 1 ; i <=9 ; i++){ for(var j = 1 ; j <= i ;j++){ write(i+"*"+j+"="+(i*j)+" ") ; } write("<br/>") ; } } </script>
格式:
for(var 遍历的变量名 in 数组名称/对象名称){
输出变量名;
}
举例:
<script type="text/javascript"> var arr = [10,20,30,40] ; //类似于Java中的静态初始化 for(var i =0 ; i < arr.length; i++){ document.write(arr[i] +" ") ; } document.write("<hr/>"); //使用for-in语句 for(var i in arr){ document.write(arr[i] +" ") ; }
</script>
语句;
1> 函数的形式参数不能有var关键字定义,否则报错;
2> 在js中函数是可以有return语句的,直接return,但是没有返回值;
3> 在js中,函数是不存在的重载概念,后面定义的函数回覆盖掉前面定义的函数;
4> 当实际参数的数量小于形式参数的数量,就会出现一个值肯定是NaN;
如果实际参数大于形式参数,那么最终会计算前面的值,然后将后面的数据丢弃掉;
![]()
Hot AI Tools
![]()
Undresser.AI Undress
AI-powered app for creating realistic nude photos
![]()
AI Clothes Remover
Online AI tool for removing clothes from photos.
![]()
Undress AI Tool
Undress images for free
![]()
Clothoff.io
AI clothes remover
![]()
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
![]()
Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution4 weeks ago By DDDWhat's New in Windows 11 KB5054979 & How to Fix Update Issues3 weeks ago By DDDWhere to find the Crane Control Keycard in Atomfall4 weeks ago By DDDRoblox: Dead Rails - How To Complete Every Challenge1 months ago By DDDHow to fix KB5055523 fails to install in Windows 11?2 weeks ago By DDD![]()
Hot Tools
![]()
Notepad++7.3.1
Easy-to-use and free code editor
![]()
SublimeText3 Chinese version
Chinese version, very easy to use
![]()
Zend Studio 13.0.1
Powerful PHP integrated development environment
![]()
Dreamweaver CS6
Visual web development tools
![]()
SublimeText3 Mac version
God-level code editing software (SublimeText3)
![]()
Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
