search
HomeWeb Front-endHTML TutorialXiaoqiang's HTML5 mobile development road (28) - JavaScript review 3

1. Basic data types

number: numeric type

string: string (note s is lowercase: string is the basic type)

boolean: Boolean type // The first three have corresponding packaging classes

null: empty type

undefined: undefined type

Test one:

<html>  
    <!--基本类型测试-->  
    <head>  
        <script>  
            function f1(){   //number类型  
        /*有返回值时也不能function void f1(){}*/  
                alert(&#39;hello&#39;);  
    /*alert(); 弹出消息框*/  
                alert(&#39;hehe&#39;);  
                var i=100;  
    //js当中字符串可用单引号也可用双引号  
                i=&#39;hello&#39;;  
        //typeof是一个运算符,可以返回变量实际存放的数据的类型  
                alert(typeof i);  
    /*js是弱类型语言 不能: number i=100; 不能在声明时指明其类型,在运行时才能确定*/  
            }  
            function f2(){   //string类型  
                var str1=&#39;hello&#39;;  
                var str2=&#39;hello&#39;;  
                if(str1==str2){  
                    alert("str1==str2");  
                }else{  
                    alert("str1!=str2");  
                }  
                var str3=&#39;100&#39;;  
                var i=100;  
                if(str3==i){ //两个=号,进行类型转换  
                    alert("str3==i");  
                }else{  
                    alert("str3!=i");  
                }  
                if(str3===i){ //三个=号,不进行类型转换  
                    alert("str3==i");  
                }else{  
                    alert("str3!=i");  
                }  
            }  
            function f3(){  //boolean类型  
                //布尔类型只有两个值:true/false;  
                var flag=true;  
                alert(typeof flag);  
              //var str="abc";  
                var str=new Object();//创建一个对象,对象会转换为true;  
                var str=null; //转换为false;  
                var str;  //undefined 转换为false;  
                //强制转换,非空的字符串转换为true,非零的数字转换为true;  
                if(str){  
                    alert(&#39;结果为真&#39;);  
                }else{  
                    alert(&#39;结果为假&#39;);  
                }  
            }  
            function f4(){  //null类型  
                var obj=null;  
            //null类型只有一个值——null;  
            //输出的结果是Object  
                alert(typeof obj);  
            }  
            function f5(){ //undefined类型  
                var obj;  
                alert(typeof obj);  
            }  
        </script>   
    </head>  
    <body style="font-size:30px;">  
        <input type="button" value="演示基本类型的使用"  
        onclick="f1();"/>  
    </body>  
</html>

Test two: parseInt

<html><span style="white-space:pre">  </span>  
    <head>  
        <script>  
        /*number--->string  
          string---->number  
        */  
            function f1(){  //字符串变数字  
        //      var str1=&#39;fsfs&#39;;     读出NaN  
        //              var str1="1234fsfs";  可以读出1234  
        //              var str1="fsfs1234";   不可以读出  
        //              var str1="22323.08";  
                var str1=&#39;1234&#39;;  
        //window.parseInt();  window可以省略  
                var n1=parseInt(str1);  
//js浮点运算会有误差,先放大x倍,再缩小x倍  
        //      var n2=parseFloat(str1);  
        //undefined + 数字 = NaN  
                alert(n1+100);  
            }  
            function f2(){  
                var n1=100;  
        //number--->Number(对应的包装类型)  再调用toString();  
                var s1=n1.toString();  
            //      var s1=n1+&#39;&#39;;  
            }  
        </script>  
    </head>  
    <body>  
        <input type="button" value="演示基本数据类型" onclick="f1();"/>  
    </body>  
</html>

Test 3: String method


length attribute: Returns the length of the string

charAt(index): Returns the specified position Characters

substring(from,to): Returns the substring

indexOf(str): Returns the position of the string in the original string

lastIndexOf(str):

match(regexp): Returns an array that matches the regular expression

Interception

function f4(){ //string的方法  
    var str1="abcdef";  
    var str2=str1.substring(1,4);  
    alert(str2);  
}

Regular

function f5(){  
    var str="asdfas12323adfasf23423";  
  //在js中用/reg/,在执行时,会将//内的内容转换成一个RegExp对象  
    var reg=/[0-9]+/g;    
  //reg中是一个对象,不是字符串,注意加一个g搜索整个字符串,还有i忽略大小写。  
    var arr=str.match(reg);  
    alert(arr);  
}

Find

function f6(){  
    var str1="sdf1223asdfasf23423";  
    var reg=/[0-9]+/;  
    //alert(typeof reg);  
    alert(reg instanceof RegExp);  
    var index = str1.search(reg);  
    alert(index);  
}

Replace

function f7(){  
    var str1="sdf444asdfadf4423";  
    var reg=/[0-9]+/g;  
    var str2 = str1.replace(reg,&#39;888&#39;);  
    alert(str2);  
}

2. Object type (array, function, others in the next article)

1. Array

The length of js array is variable

JS array elements are arbitrary (different types of data can be mixed and stored)

<html>  
    <head>  
        <script>  
            function f1(){  //创建数组的第一种方式  
                var arr=new Array();  //()可以省略不写  
                arr[0]=1;  
                arr[3]=2;  
                arr[5]=&#39;abc&#39;;  
                alert(arr.length);  
                alert(arr[1]);  
                alert(arr[3]);        
            }  
            function f2(){ //第二种方式  
                var arr=[];  
                arr[0]=1;  
                arr[3]=22;  
                var arr=[1,2,3,4,5,6];  
                arr[7]=11;  
                alert(arr.length);  
            }  
            function f3(){ //多维数组的创建  
                var arr = new Array();  
                arr[0]=[1,2,3,4,5];  
                arr[1]=[6,7,8,9,10,11,12,13];  
                arr[2]=[14,15,16,17,18,19];  
                for(var i=0;i<arr.length;i++){  
                    for(j=0;j<arr[i].length;j++){  
                        alert(arr[i][j]);  
                    }  
                }  
            }  
            function f4(){ //数组常用的属性和方法  
                var arr=[11,22,33,44];  
                arr.length=2;  //数组的长度可以写,后面的被砍掉  
                alert(arr);  
                alert(typeof abc);  
            }  
        </script>  
    </head>  
    <body>  
        <input type="button" value="数组的使用" onclick="f4()"/>  
    </body>  
</html>

Some functions in the array

<html>  
    <head>  
        <script>  
            function f1(){  
                var a1 = [1, 2, 3];  
                var str = a1.join(|);  
                alert(str);  
            }  
            function f2(){  
                var a1 = [1, 2, 3];  
                 var a2 = [4, 5, 6];  
                 var a3 = a1.concat(a2); //数组连接  
                 alert(a3);  
            }  
            function f4(){  
                var a1 = [1, 2, 3];  
                var a2 = a1.reverse(); //是对原有数组翻转  
                alert(a2);  
                alert(a1);  //原数组变了  
            }  
            function f5(){  
                var a1 = [1, 2, 3, 4, 5, 6];  
                var a2 = a1.slice(2,4); //对数组截取  
                alert(a2);  
                alert(a1); //原数组没有变化  
            }  
            function f6(){  
                var a1 = [5, 1, 7, 2, 8];  
                var a2 = a1.sort(); //从小到大  
                alert(a2);  
            }  
            function f7(){  
                var a1 = [15, 111, 7, 22, 88];  
                var a2 = a1.sort(); //默认按照字典顺序排序  
                alert(a2);   
            }  
            function f8(){  
                var a1 = [15, 111, 7, 22, 88];  
                var a2 = a1.sort(function(t1, t2){  
                    return t2-t1;  
                });   
                alert(a2);   
            }  
            function f9(){  //按照字符串长度排序  
                var a1 = [&#39;abc&#39;, &#39;bb&#39;, &#39;casd&#39;, &#39;a&#39;];  
                var a2 = a1.sort(function(t3, t4){  
                    return t4.length-t3.length;  
                });   
                alert(a2);   
            }  
        </script>  
    </head>  
    <body>  
        <input type="button" value="click me" onclick="f9()"/>  
    </body>  
</html>

2. Function

Define a function

function function name (parameter){

function body

}

Several issues to pay attention to

a. There cannot be a return Type declaration, but can have return value.

b. The essence of a function is an object, an instance of the Function type. The function name is a variable that stores the address of the object (the function name is a variable).

c. In the function Internally, you can use the arguments object to access parameters

d. Functions cannot be overloaded

<html>  
    <!--函数的使用-->  
    <head>  
        <script>  
            function add(a1, a2){  
                return a1+a2;  
            }  
            function test(){  
                var sum = add(1, 1);  
                alert(sum);  
            }  
            function test2(){  
            //  alert(typeof add);  
                alert(add instanceof Function);  //函数是Function类型的实例  
                var f2 = add;           //存放的是对象的地址  
                add = null;              //add指向空  
                var sum = f2(1, 1);         //等价于 add(1, 1);  
                alert(sum);  
            }  
            function add2(arg1, arg2){  
                //return  arg1 + arg2;  
                return arguments[0]+arguments[1];  
            }  
            function add3(arg1, arg2){  //首先指向一个对象  
                return arg1+arg2+100;  
            }  
            function add3(){    //指向了另一个对象  
                return arguments[0]+arguments[1];  
            }  
            function test3(){  
                //var sum = add2(1);         //结果为NaN,因为arg2是undifined  
                //var sum(1, 1, 1);    //结果为2  
                //var sum=add(1, 1);  
                //var sum = add2(1, 1, 1);  
                var sum = add3(1, 1);  
                alert(sum);  
            }  
        </script>  
    </head>  
    <body>  
        <input type="button" value="click me" onclick="test3()"/>  
    </body>  
</html>

For other Object types, please see the next article

The above is Xiaoqiang’s HTML5 mobile development path (28)——JavaScript Review 3 content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!