Getting started with JavaScript basics

Getting started with JavaScript basics

1. Where to write JavaScript

Generally divided into three types: inline, embedded, and external

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 2. Embedded js -->
    <script>
        alert('popup')
    </script>
    <!-- 3. External js -->
    <script src="outside.js"></script>
</head>
<body>
    <!-- 1. Inline js, written directly inside the element -->
    <input type="button" value="button" onclick="alert('I am a button')">
</body>
</html>

2. Commonly used input and output statements in JavaScript

1. The browser pops up a warning box:

alert(msg)

2. The browser console prints out information:

console.log(msg)

The output information can be seen in the console in F12

3. The browser pops up an input box, allowing the user to enter:

prompt(info)

Variables

1. Use of variables:

1. Declare variables 2. Assign values

The data variable type of js is confirmed according to the value on the right side of the equal sign when the program is running.

         var a; //declare variable a
         a=1;
         alert(a) 

2. Read the input value (cin>>)

 <script>
        var a = prompt();
        // a=1;
        alert(a); 
    </script>

3. Maximum and minimum values ​​of numbers in JavaScript, and infinity

Number.MAX_VALUE Number.MIN_VALUE

Infinity -Infinity

4. Use isNaN to determine whether it is a number

isNaN(11) returns false if it is not a number, returns true

5.typeof detects variable data type

<script>
    var num =10;
    console.log(typeof num);//Detect the data type of num</script>

6. Data type conversion

6.1 Converting to a string

//1. toString()
var num=1;
alert(num.toString());
//2.String() forced conversion var num = 1;
alert(String(num));
//3. Plus sign concatenation string var num = 1;
alert(num+"string");

6.2 Convert to digital type

    <script>
        // 1.parseInt gets an integer var age = prompt("input your age");
        console.log(parseInt(age))
        // 2.parseFloat gets a floating point number console.log(parseFloat(age));
        // 3. Number() forced conversion console.log(Number(age));
        // 4. Using arithmetic operations - * /console.log('12'-0);
        console.log('12'-'10') // Output is a digital 2
    </script>

6.3 Convert to Boolean

Using the Boolean() Function

Values ​​representing empty or negative values ​​will be converted to false, such as 〝O , NaN , null , and undefined

All other values ​​will be converted to true.

Operators

Operator precedence

5. Function

1. Function usage: declare the function first, then call the function

function function name(){
        //Function body}

2. Function parameters

Divided into formal parameters and actual parameters

3. Function return value

Use return to return the value

The code after return will not be executed and can only return one value

4.Use of argument

When we are not sure how many parameters are passed, we can use arguments to get them. In JavaScript, arguments is actually a built-in object of the current function. All functions have a built-in arguments object that stores all the passed parameters.

function fn(){
    console.log(argument); //It stores all the passed arguments}
fn(1,2,3);
 

In the browser

The argument display form is a pseudo-array, but it has the length attribute of the array and is stored in an indexed manner. But it does not have some array methods such as pop push

5. Two ways to declare a function

1. Naming functions

 function fn(){
    //Function body}
    fn();

2. Anonymous functions

var fun = function(){
    //Function body}
fun();

Fun is a variable name, not a function name, but function expressions (anonymous functions) can also pass parameters.

6. Scope

1. JavaScript Scope

Generally speaking, the names used in a program code are not always valid and available, and the scope of code that limits the availability of the name is the scope of the name. The use of scope improves the locality of program logic, enhances program reliability, and reduces name conflicts.

Generally divided into global scope and local scope

The global scope is within the entire script tag, and the local scope is within the function

It is worth noting that there is no block-level scope in js, that is, if a variable is declared in an if statement, it can also be called outside.

if(3>5){
    var num = 1;
}
console.log(num);

It can be compiled in the browser without any errors.

2. Scope of variables

Global variables are also in the script tag. If there is no declaration in the function, the variable directly assigned is also a global variable.

function fn(){
    num2 = 10; // global variable var num1 = 1; // local variable }

Global variables can also be used in functions

3. Scope Chain

Based on the mechanism that inner functions can access outer function variables, chain search is used to determine which data can be accessed by inner functions.

Adopt the principle of proximity.

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of basic interaction of javascript
  • Detailed explanation of Javascript basics
  • Javascript Basics: Detailed Explanation of Operators and Flow Control
  • Detailed explanation of basic syntax and data types of JavaScript
  • Javascript basics about built-in objects
  • JavaScript functional programming basics

<<:  Semantics, writing, and best practices of link A

>>:  Several methods and advantages and disadvantages of implementing three-column layout with CSS

Recommend

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...

Optimize the storage efficiency of BLOB and TEXT columns in InnoDB tables

First, let's introduce a few key points about...

4 ways to implement routing transition effects in Vue

Vue router transitions are a quick and easy way t...

Docker uses root to enter the container

First run the docker container Run the command as...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

js realizes horizontal and vertical sliders

Recently, when I was doing a practice project, I ...

Detailed explanation of MySQL 8's new feature ROLE

What problems does MySQL ROLE solve? If you are a...

How to deploy MySQL 5.7 & 8.0 master-slave cluster using Docker

> Deploy MySQL 5.7 cluster master & slave ...

Detailed explanation of Nginx log customization and enabling log buffer

Preface If you want to count the source of websit...

webpack -v error solution

background I want to check the webpack version, b...