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
Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

What are different types of lists in HTML?What are different types of lists in HTML?Apr 28, 2025 pm 05:36 PM

Article discusses HTML list types: ordered (<ol>), unordered (<ul>), and description (<dl>). Focuses on creating and styling lists to enhance website design.

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.