Detailed explanation of Javascript basics

Detailed explanation of Javascript basics

variable

  • Basic syntax
var age=10; //Declare a variable named age and assign a value to it, which is called variable initialization

var is a JS keyword used to declare variables. We can also update variables according to the ideas of other programming languages, and declare multiple variables at the same time. In this case, we only need to use one var and separate multiple variable names with English commas.

  • Naming conventions

It is composed of letters (A-Za-z), numbers (0-9), underscores (_), and dollar signs ($), such as: usrAge, num01, _name
Strictly case sensitive. var app; and var App; are two variables that cannot start with a number and must be a single word with no spaces in between. 18age is wrong and cannot be a keyword or a reserved word. For example: var, for, while
Variable names must be meaningful.
Follow camelCase naming convention. The first letter of the word should be lowercase, and the first letter of the following word should be uppercase. myFirstName

Data Types

  • Data Type Introduction

JS is a weakly typed or dynamic language, which means that there is no need to declare the value of a variable in advance. The type will be automatically determined during the execution of the program. The data type of a variable is determined by the JS engine based on the data type of the variable value on the right side of the equal sign, which means that the same variable can be used as different types.

var age = 10; // This is a number var age="10" // This is a string

JS data types are divided into simple data types and complex data types
Number String Boolean Undefined Null (Simple data types are also called primitive data types)
Object Array Date function (complex data types are also called reference data types)

However, in the new syntax of ES6 and H5, the Symbol simple data type has been added (which will be discussed later)

insert image description here

  • Number

Common systems include binary, octal, decimal, and hexadecimal. In JS, add 0 in front of octal and 0x in front of hexadecimal.

//1. Octal number sequence range: 0~7
var num1 = 07; // corresponds to decimal 7
var num2 = 019; // corresponds to decimal 19
var num3 = 08; // corresponds to decimal 8
//2. Hexadecimal number sequence range: 0~9 and A~F
var num = 0xA; 

Maximum value: Number.MAX_VALUE, this value is: 1.7976931348623157e+308
Minimum value: Number.MIN_VALUE, this value is: 5e-32

alert(Number.MAX_VALUE); // 1.7976931348623157e+308
alert(Number.MIN_VALUE); // 5e-32

Three special values
Infinity, representing infinity, is greater than any value
-Infinity, representing infinitesimal, smaller than any value
NaN, Not a number, represents a non-numeric value

isNaN() determines whether a variable is a non-numeric type.

var usrAge = 21;
var isOk = isNaN(userAge);
console.log(isNum); // false , 21 is not a non-number var usrName = "andy";
console.log(isNaN(userName)); //true, "andy" is a non-number
  • String

Use single quotes to represent string quotations. You can also nest string quotations. You can nest double quotes in single quotes or single quotes in double quotes. However, you cannot mix single and double quotes. The escape characters are as follows.

insert image description here

Get the length of the string
String concatenation string + any type = new string after concatenation. If two values ​​are added, the result is a value.

  • Boolean

true and false, when adding a Boolean value to a number, the value of true is 1 and the value of false is 0

  • Undefined

A variable that is declared but not assigned a value will have a default value of undefined.
If you use an undeclared variable, an error will be reported.

var variable;
console.log(variable); // undefined
console.log('hello' + variable); // hello is undefined
console.log(11 + variable); // NaN
console.log(true + variable); // NaN
  • Null
var vari = null;
console.log('hello' + vari); // hello null
console.log(11 + vari); // 11
console.log(true + vari); // 1
 
  • Data type conversion

The data obtained using the form or prompt is of string type by default. In this case, you cannot simply perform addition operations directly, but need to convert the data type of the variable. In layman's terms, it converts a variable of one data type into another data type.

Convert to string type

insert image description here

Convert to digital type

insert image description here

Convert to Boolean type

insert image description here

Empty and negative values ​​will be converted to false, such as '', 0, NaN, null, undefined
All other values ​​will be converted to true.

console.log(Boolean('')); // false
console.log(Boolean(0)); // false
console.log(Boolean(NaN)); // false
console.log(Boolean(null)); // false
console.log(Boolean(undefined)); // false
console.log(Boolean('小白')); // true
console.log(Boolean(12)); // true

Extension Points

Interpreted languages ​​and compiled languages.
A computer must compile (using a translator) the programming language into machine language before it can execute the program.
There are two ways for a translator to translate into machine language, one is compilation and the other is interpretation. The difference lies in the time point of translation.
The compiler compiles the code before it is executed and generates an intermediate code file. The interpreter interprets it in real time at runtime and executes it immediately.

insert image description here

  • Identifiers are names that developers give to variables, parameters, and functions. Identifiers cannot be keywords or reserved words.
  • Keywords refer to words that have already been used by JS itself and cannot be used as variable names and method names.

Includes: break, case, catch, continue, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with

  • Reserved words are reserved keywords that may become keywords in the future.

Including: boolean, byte, char, class, const, debugger, double, enum, export, extends, fimal, float, goto, implements, import, int, interface, long, mative, package, private, protected, public, short, static, super, synchronized, throws, transient, volatile, etc.

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
  • 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
  • Getting started with JavaScript basics

<<:  MySQL 8.0.23 Major Updates (New Features)

>>:  Detailed explanation of where the image pulled by docker is stored

Recommend

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

MySQL 8.0.18 installation and configuration method graphic tutorial (linux)

This article records the installation and configu...

MySQL query redundant indexes and unused index operations

MySQL 5.7 and above versions provide direct query...

5 tips for writing CSS to make your style more standardized

1. Arrange CSS in alphabetical order Not in alphab...

mysql batch delete large amounts of data

mysql batch delete large amounts of data Assume t...

Implementation steps for enabling docker remote service link on cloud centos

Here we introduce the centos server with docker i...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

WeChat applet implements form verification

WeChat applet form validation, for your reference...

10 Tips for Mobile App User Interface Design

Tip 1: Stay focused The best mobile apps focus on...

Basic usage tutorial of IPTABLES firewall in LINUX

Preface For production VPS with public IP, only t...

How to implement controllable dotted line with CSS

Preface Using css to generate dotted lines is a p...

TypeScript enumeration basics and examples

Table of contents Preface What are enums in TypeS...

base target="" specifies the target of the base link to open the frame

<base target=_blank> changes the target fram...

HTML Tutorial: Unordered List

<br />Original text: http://andymao.com/andy...