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

Implementing a distributed lock using MySQL

introduce In a distributed system, distributed lo...

A detailed introduction to Tomcat directory structure

Open the decompressed directory of tomcat and you...

Vue batch update dom implementation steps

Table of contents Scene Introduction Deep respons...

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

js precise calculation

var numA = 0.1; var numB = 0.2; alert( numA + num...

Mysql example of splitting into multiple rows and columns by specific symbols

Some fault code tables use the following design p...

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

Users need to know why

When I was in the securities company, because the ...

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new w...

How to publish static resources in nginx

step Place the prepared static resource files in ...

HTML embed tag usage and attributes detailed explanation

1. Basic grammar Copy code The code is as follows...

How to use the Clipboard API in JS

Table of contents 1. Document.execCommand() metho...

vue-amap installation and usage steps

I have previously shared the usage of asynchronou...

How to install Docker on Windows 10 Home Edition

I recently used Docker to upgrade a project. I ha...

Implementation of grayscale release with Nginx and Lua

Install memcached yum install -y memcached #Start...