Lession01 javascript 簡介

代碼注釋

單行注釋 //單行注釋
多行注釋 /*多行注釋*/

書寫位置

HTML頁面書寫

<body>
    <script>
        console.log("Hello world!");//在控制臺(tái)輸出字符串
        alert("你好,世界");//彈出消息框
        //可以將數(shù)據(jù)輸出到頁面上
        document.write("我也要說一下:你好,世界!");
        
        function getContent(){
          alert("在標(biāo)簽中定義的方法!");
        }

    <script>
<!--按鈕的單擊事件,注意單雙引號(hào)的使用-->
<button onclick="window.alert('hello world 你好,世界');">彈出警告框</button>
<!-- 按鈕點(diǎn)擊事件調(diào)用 -->
<button onclick="getContent();">第二個(gè)按鈕</button>

</body>

引入外部js文件

<script src="js/myjs.js"><script>
<button onclick="getJS();"></button>
//外部js文件
function getJS(){
  alert("調(diào)用外部js顯示")
}

數(shù)據(jù)類型

基本數(shù)據(jù)類型 引用數(shù)據(jù)類型
數(shù)值類型 number 字符串類型 string
布爾類型 Boolean 數(shù)組類型 Array
未定義類型 undefined 對象類型 Object
空數(shù)據(jù)類型 null
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>變量的定義</title>
    </head>
    <body>
        <script>
            var salary;  //定義變量
            salary = 2000;  //給變量賦值
            var name = "布什"; 
            var price = 2.5;
            var isFamle = true;
            var obj = null;
            //使用document.write()方法,可以將數(shù)據(jù)輸出到頁面
            document.write("您的薪水是:"+salary+"元!salary的類型是:"+typeof(salary)+"<br>");
            document.write("我的名字是:"+name+"!name的的類型是:"+typeof(name)+"<br>");
            document.write("蘋果的價(jià)格是:"+price+"元!price的類型是:"+typeof(price)+"<br>");
            document.write("isFamle的類型是:"+typeof(isFamle)+"<br>");
            document.write("obj的類型是:"+typeof(obj)+"<br>");
            
        </script>
    </body>
</html>

數(shù)據(jù)類型轉(zhuǎn)換

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>類型轉(zhuǎn)換</title>
    </head>
    <body>
        <script>
            var address;
            //變量未給初始值時(shí),string類型為 "undefined"
            document.write("address的值是:"+address+"<br>");
            var phone = null;
            //變量為null時(shí) ,字符串的結(jié)果是null
            document.write("phone的值是:"+phone+"<br>");
            var sname= "HOPE";
            //將變量轉(zhuǎn)換成整數(shù)類型
            var snameResult = parseInt(sname);
            document.write("將姓名轉(zhuǎn)換為數(shù)字的結(jié)果是:"+snameResult+"<br>");
            var age = "20";
            //將變量轉(zhuǎn)換成整型數(shù)字
            var ageResult = parseInt(age);
            document.write("將年齡轉(zhuǎn)換成數(shù)字的結(jié)果是:"+ageResult);
            
        </script>
    </body>
</html>

混合計(jì)算

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>混合計(jì)算</title>
    </head>
    <body>
        <script>
            var x = 100;
            var y = 10.2;
            var s = "字符串";
            var b = true;
            var n = null;
            //整數(shù)+小數(shù)=小數(shù)
            //document.write(x+y);
            //整數(shù)+字符串=字符串
            //document.write(x+s);
            //整數(shù)+布爾值=整數(shù)
            //document.write(x+b);
            //整數(shù)+空值=整數(shù)
            //document.write(x+n);
            //小數(shù)+字符串=字符串
            //document.write(y+s);
            //小數(shù)+布爾值=小數(shù)
            //document.write(y+b);
            //小數(shù)+空值=小數(shù)
            //document.write(y+n);
            //字符串+布爾值=字符串
            //document.write(s+b);
            //字符串+空值=字符串
            //document.write(s+n);
            //布爾值+空值=整數(shù)
            document.write(b+n)
        </script>
    </body>
</html>

算術(shù)運(yùn)算符

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>算術(shù)運(yùn)算符</title>
    </head>
    <body>
        <script>
            var x=5;
            var y = 4;
            document.write("和是:"+(x+y)+"<br>");
            document.write("差是:"+(x-y)+"<br>");
            document.write("積是:"+(x*y)+"<br>");
            document.write("商是:"+(x/y)+"<br>");
            document.write("余數(shù)是:"+(x%y)+"<br>");
            document.write((x++)+"<br>");
            document.write((x)+"<br>");
        </script>
    </body>
</html>


比較運(yùn)算符

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>比較運(yùn)算符</title>
    </head>
    <body>
        <script>
            //==是值相等,===是值和類型都相等
            // if(1==="1"){
            //  document.write(true);
            // }else{
            //  document.write(false);
            // }

            //小寫字母大于大寫字母
            // if("a" > "A"){
            //  document.write(true);
            // }else{
            //  document.write(false);
            // }
            // //較長的字符串 大于 較短的字符串
            // if("abc" < "abcd"){
            //  document.write(true);
            // }else{
            //  document.write(false);
            // }    
            // // //先出現(xiàn)在字母表的字符小于后面的字符
            // if("a" < "b"){
            //  document.write(true);
            // }else{
            //  document.write(false);
            // }

            if("abc" > "abC"){
                document.write(true);
            }else{
                document.write(false);
            }
        </script>
    </body>
</html>


邏輯運(yùn)算符和條件運(yùn)算符

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>邏輯運(yùn)算符和條件運(yùn)算符</title>
    </head>
    <body>
        <script>
        //邏輯或:有一個(gè)為真則為真
            if(1==1 || 1==2){
                document.write(true)
            }else{
                document.write(false);
            }
            //邏輯與:有一個(gè)為假則為假
            if(1==1 && 1==2){
                document.write(true)
            }else{
                document.write(false);
            }
            
            //條件運(yùn)算符  (條件)? 條件真的值:條件假的值
            var x="";
            //獲取當(dāng)前小時(shí)
            var time = new Date().getHours();
            //document.write(time);
            x=(time<18) ? "上課時(shí)間" : "休息時(shí)間";
            document.write("x="+x);
        </script>
    </body>
</html>


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML :標(biāo)記語言 JavaScript :編程語言 markdown 序言 JavaScript發(fā)展歷史(JS...
    蝸牛的愿望正在緩沖中閱讀 230評論 0 0
  • 學(xué)習(xí)目標(biāo): 掌握編程的基本思維 掌握編程的基本語法 JavaScript基礎(chǔ) 網(wǎng)頁、網(wǎng)站和應(yīng)用程序 網(wǎng)頁:單獨(dú)的一...
    iTruda閱讀 683評論 0 0
  • 簡介 JavaScript 是世界上最流行的語言之一,是一種運(yùn)行在客戶端的腳本語言 (Script 是腳本的意思)...
    一往情深_e32c閱讀 355評論 0 0
  • 簡介 JavaScript 是世界上最流行的語言之一,是一種運(yùn)行在客戶端的腳本語言 (Script 是腳本的意思)...
    le_u閱讀 110評論 0 0
  • 今天主要內(nèi)容: JavaScript介紹 ECMAScript基本調(diào)試 變量與數(shù)據(jù)類型 運(yùn)算符 分支語句與循環(huán)語句...
    小心草叢閱讀 291評論 0 0

友情鏈接更多精彩內(nèi)容