Home Web Front-end JS Tutorial Introduction to JavaScript language core data types and variable usage_Basic knowledge

Introduction to JavaScript language core data types and variable usage_Basic knowledge

May 16, 2016 pm 05:24 PM
variable type of data

Any programming language has its own language core. Like many programming languages, JavaScript also has its own language core. The core part of the language is generally called the most basic part of JavaScript. As the saying goes, the beginning of everything is difficult, and learning JavaScript is also the same. There is an old saying that a good start is half the battle. Understanding and learning the core parts of the JavaScript language is a very good start on the path to learning JavaScript.

The following is a piece of code, which mainly introduces JavaScript data types and variables:

Copy code The code is as follows:

//In the script tag, all content after // is called annotation
//The function of annotation is generally to explain a piece of JavaScript so that other web front-end developers can Engineers may understand more clearly when reading this code

//A variable is a symbolic name of a value. Through the names of these variables, we can also generally know what this variable is used for and what it belongs to. Variable type
//It is very simple to distinguish variables. There is "var" in front of the variable, which means that the variable is declared through "var".

var m; //Declare a variable m

//Generally we assign the value to be declared to the variable with the equal sign
var m = 10; //The current variable m Equal to 10

m //Get the value just declared through the variable m

//alert(m) //Use the alert() function to pop up the value of m in the browser

//JavaScript data types: boolean, number, string, underfind, function, array, object

var n = 1; //Number
n = 0.01 //Integer sum Real numbers are all numeric types

var s = "Hello"; //A string composed of text within double quotes
s = 'Guoan'; //Consisted of text within single quotes String

var b = false; //Wrong Boolean value
b = true; //Correct Boolean value

var z = null; //A null value , is a special type. After typeof is the object

var u; //underfind

var j = { //An object representing json
li :3, //Attributes The value of "li" is 3
meng :4 //The value of attribute "meng" is 4
}

j["li"] //Access the value in json through []
j.li //Access the value in json through .
j.long = 5 //Create new attributes through assignment method
j.ai = { //You can create new json through assignment
xin : 33
}
j.kong = {} //{} represents an empty object, which has no attributes

j.ai.xin //Access the new json through. Attribute

var a = [2,3,2] //An object representing an array

a[0] //[] in the array represents the position in [], and the array starts from Starting from 0, so a[0] is the first element of the array
a.length //length represents the number of array a, 3
a[a.length-1] //represents the last element in the array An element
a[9] = 2; //Add a new element using assignment method

//If a = [], then it means that there are zero elements in the array, a.length = 0

a[0] = {
li : 333
}

//json can contain arrays, and arrays can also contain json

/ /alert(a[0]["li"])

In the above code, you can define objects through "[]", "{}", ".", or through "[ ]", "{}", "." to change the contents of the array or object. You can also read some data in the object through "[]", "{}", and ".". The following set of codes is about operators:
Copy the code The code is as follows:

//You can use budget characters in JavaScript to perform operations between two numbers and generate new values ​​
//The following are some of the more common budget characters, such as " ", "-" , "*", "/"

//1. Introducing operators
10 10 //Addition, 20
10 * 10 //Multiplication, 100
10 - 10 // Subtraction, 0
10 /10 //Division, 1

var j = { //An object representing json
li :3, //The value of attribute "li" is 3
meng :4 //The value of attribute "meng" is 4
}

j["li"] - j.meng //Attribute li in json j minus attribute meng in json j , the result is -1

"10" "10" //Addition can concatenate strings, the result is 100

//2. Some abbreviated operators are defined in JavaScript

var num = 0 //Define a number

num; //Represents self-increment, num = num 1;
num--; //Represents self-decrement, equivalent to num = num - 1;
num = 2; //Represents self-increment by 2, equivalent to num = num 2;
num *= 8; //Represents self-proclaimed 8, equivalent to num = num * 8;

//3. Operators for judgment

var a = 1,b = 2; //An equal sign represents copying, and two variables are separated by ",", which represents simultaneous declaration

a == b; //The result is false, does it mean that a and b are equal?
a != b; //The result is true, does it mean that a and b are not equal?
a < b; //The result is true, does it mean that a is less than b?
a <= b; //The result is true, does it mean that a is less than or equal to b?
a > b; //The result is false, which means Is a greater than b?
a >= b; //The result is false, which means is a greater than or equal to b?
"two" == "three"; //true "tw" index in the alphabet Greater than "th"
false > (a > b) //The result is true, which means comparing false with false

//4. Logical operators

(a = = 2) && (b == 3) //The result is true. Is a equal to 2 and b equal to 3? && means and
a > 2 || b > 2 //The result is true, the first one is false and the second one is true, because || means or
!(a == b) //The result is true. ! It means negation.

Operators that only calculate a value and do not affect any operation are called expressions and do not change the running status of the program. The statement does not contain a value, but it changes the running state. Since the statement changes the running status, a semicolon is added after it.

Each function has its own name. A certain function can be called and executed through the name. It can be defined once and called multiple times. Below is a simple example of a function.
Copy code The code is as follows:

//1. A function is a JavaScript code end with parameters. It can be defined once, called multiple times, or with parameters

var a = 3; //Declare a value Variable a is 3;

function fn1 (n) { //A function with parameter n named fn1
return n 1; //Return a value that is one greater than the value passed in
}

fn1(a) //The result is 4. Since the value of a just declared is 3, when calling the function, a 1 is executed, which is 3 1

var fOne = function(m) { //Function is also a data type, so you can also assign a variable to a function
return m*m; //Return a value and perform calculations on parameters* parameters
}

fOne(a) //The result is 9

//2. Method, assign the function to the attribute of the variable

var arr = []; //Create a new array
arr.push(1,2,3); //Use the push() method to add elements to the arr array from behind
arr.reverse(); //Use the reverse() method to sequence the elements in the array Reverse

var points = [ //Declare an array whose elements are json
{a : 0,b : 0},
{a : 1,b : 1}
]
points.dist = function () { //Define a method to calculate the distance between two points in the declared array

var p1 = this[0]; //Use this to get the current Array reference
var p2 = this[1]; //And assigned to two new variables
var a = p2.a - p1.a; //Distance on the x-axis
var b = p2.b - p1.b; //Distance on the y-axis

return Math.sqrt(a*a b*b) //Use sqrt() in Math() to calculate the square root to get two points The distance between

}

alert(points.dist()) //The result is 1.414

//3. Control statement
//Conditional statement and The loop statement is called a control statement

function abs (m) { //Find the absolute value function

if (m >= 0) { //If the comparison result is true
return m; //Return m
}else { //If the comparison result is false
return -m; //Return -m
}

}

function factorial (n) { //Function to calculate factorial

var num = 1; //Declare a variable with a value of 1

while (n > 1) { //When ( When the expression in ) is true, execute the code in loop {}

num *= n; //Equivalent to num = num * n
n--; //Equivalent to n = n -1

}

return num //Return the result of factorial

}

factorial(4) //The result is 24

function factorialFor (n) { //Use for loop to implement factorial
var i, num = 1; //Declare variable i, and declare variable num with value 1

for (i=2; i <= n ; i ) { //Loop i from 2 to n
num *= i; //Loop body, you can omit it when there is only one sentence in the loop body {}
}

return num; //Return the calculated factorial table

}

factorialFor(5)

. As can be seen from the function example, Whether it is a while loop or a for loop, whether it is a judgment statement or a loop statement, it can be regarded as a control statement. Control what will happen through certain conditions.

After introducing functions, let’s briefly introduce object-oriented.
Copy code The code is as follows:

//Defining a constructor means first creating an initialization Object

function Point (x,y) { //The first letter of the constructor name must be capitalized
this.x = x; //this represents the initialization object
this .y = y; //Save the function parameters into the properties of this initialization object
} //There is no need to return in the constructor, return something

//Use the new keyword and construct Function, create a new object
var p = new Point(1,1); //Create a point with plane coordinates (1,1)

//By assigning a value to the constructor prototype, To add a method to the newly created object of Point
Point.prototype.r = function () {

return Math.sqrt(
this.x*this.x this.y*this.y
); //Use the sqrt() method in Math to perform the square root operation. this refers to the object that calls the method

}

p.r() //The result is 1.414

The above example is to teach you how to define a request Points for square root method. There are some differences between the object orientation of JavaScript and the object orientation of other programming languages. We can only know the specific differences by continuing to study them.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

Detailed explanation of variable scope in Golang functions Detailed explanation of variable scope in Golang functions Jan 18, 2024 am 08:51 AM

Detailed explanation of variable scope in Golang functions In Golang, the scope of a variable refers to the accessible range of the variable. Understanding variable scope is important for code readability and maintainability. In this article, we will take a deep dive into variable scope in Golang functions and provide concrete code examples. In Golang, the scope of variables can be divided into global scope and local scope. The global scope refers to variables declared outside all functions, that is, variables defined outside the function. These variables can be

See all articles