Beginner js (JavaScript)

Say goodbye to us who draw webpages, and officially enter the learning of js. In fact, we have just started, but I feel a little difficult, but I hope everyone will persist and digest it!

What is JavaScript?

JavaScript is an object-based and event-driven scripting language, which is composed of ecmascript (standard), bom (browser object) and dom (element object).

And today let's learn about ecmascript (standard)!

ECMAScript is a syntax standard

Includes syntax, variables and data types, operators, logical control statements, keywords, reserved words, objects 

1. How to use

1. Built-in

<body>
  <script type="text/javascript">
    /* 在文档中写入 */
   document.write('kw52初学javascript')
        /* 可以加上标签 */
   document.write('<h1>kw52初学javascript</h1>')
  </script>
</body>

2. Externally import js

<body>
    <!-- 外部引入js -->
    <script src="./js/1.js" type="text/javascript"></script>
</body>

3. Use js directly in html

<body>
    <!-- 直接在html中使用js -->
    <!-- ★使用按钮点击 执行document.write 会把页面上其他的内容覆盖 -->
    <input type="button" value="点我"  onclick="javascript:document.write('<h1>你好</h1>')">
    <!-- alert('输入弹出的内容') 弹出框  -->
    <input type="button" value="点我看看"  onclick="javascript:alert('欢迎光临')">
</body>

<script>…</script> can be included anywhere in the document,

★Just make sure that these codes have been read and loaded into memory before being used.


Two, js variables

  To declare a variable use the keyword var

1. Declare variables first and then assign values

<body>
    <script>
        var width //width只是名字,想起什么名字都可以
        width = 100
        alert(width)
    </script>
</body>


2. Declaring and assigning variables at the same time

<body>
    <script>
        var width = 100
        document.write(width)
    </script>
</body>


3. Use the sign to declare the assignment at the same time

☆Only the last one can be assigned, the previous variable is the initial value undefined

<body>
    <script>
        var width,height = 100
        alert(width)  //undefined
        alert(height)  //100
        // undefined(初始值)
    </script>
</body>

 


4. No declaration, direct assignment

☆It will turn the width variable into a global variable

<body>
    <script>
        width = 100
        document.write(width)
    </script>
</body>


3. Data type

         JS provides the typeof operator to detect the data type of a variable 

         Usage example: typeof age or typeof(age) 

         There is a case where the addition of the + sign represents splicing

1、undefined

Variable a has no initial value and will be assigned undefined

<body>
     <script>
        var a;
        document.write(a);
    </script>  
</body>


 2、null

Represents an empty value, equal to the undefined value

 typeof null ==> object (a problem left over from history)

Indicates whether the comparison values ​​are equal

<body>
     <script>
        var age = null;
        // document.write(age);
        document.write(null==undefined)
    </script>   
</body>


3. number digital type

 Divided into integers and floating point numbers (with a decimal point)

<body>
     <script>
        var scorel1 = 90;  //整数
        var scorel2 = 95.5; //浮点数
        document.write(scorel1 + '<br>')
        document.write(scorel2 +  '<br>')
        document.write(typeof scorel1)
    </script>    
</body>


 4、bull wage

Boolean: true and false

true means equal

false means not equal


5. string string

Text enclosed in quotes (single or double)


6、symbol

es6 new type symbol

Represents a unique number (not equal to any number, unless it is himself) 


7. object object type 

Guess you like

Origin blog.csdn.net/weixin_68485297/article/details/124222865