JavaScript Syntax
ii. Types of data
The program divides these quantities and values into several categories, each of which is called the name of each class, and what the characteristics are called data types.
1. Strings (String)
A string consists of 0 or more characters, including letters, numbers, punctuation, and spaces;
The string must be enclosed in quotation marks (single or double quotes).
var mood = "Happy"; var mood = ' happy ';
The content enclosed in quotation marks is the type of string;
If the contents have single quotes and double quotes, use the escape character "\" to: Resolve conflicts
2. Value (number)
var age =; var price = 20.50;
The numbers in life are also distinguished by numeric and string types:
As a number, a number, such as age, price, is the value type (number);
As a number, such as identification number, school number, is the string type (string);
3. Boolean Type (Boolean)
Boolean type data can only have two values: true and false;
The Boolean value of True and false is the logical inside of the true and false, but also in the computer language inside the 0, 1;
var true ; var false;
Note: Unlike strings, do not enclose Boolean values in quotation marks. A Boolean value of false and the string "false" are two different things.
4. Objects (object)
JavaScript is an object-oriented language, so you can use object-oriented thinking to make an object a data entity that is composed of a collection of properties and methods that are related to each other.
For example: Date Day Object
Date objects are used to process dates and times
//Create a new Date object and assign a value to the variable todayvarToday =NewDate;//Use the Date object to get the year, and then assign a value to thevarYear =today.getfullyear ();//Use the Date object to get the month of the month , and then assign the value to the variable, monthvarmonth = Today.getmonth () + 1;//Use the Date object to get to the number of days to get the date today, then assign to the variable dayvarDay =today.getdate ();//output year, month, date to pagedocument.write (year+ "year" +month+ "month" +day+ "Day");//Time Division secondsvarHours =today.gethours ();varminutes =today.getminutes ();varseconds =today.getseconds ();d Ocument.write (Hours+ "Time" +minutes+ "+seconds+" seconds ");
5.Array arrays
Variables are usually stored only one content, so the variable is a single container;
An array can generally have one or more values, so the array is a large container.
Components of the array:
An array is actually a multi-container consisting of multiple (key-value)
The index of the array, which is the default starting at 0
// Way One var New Array (1,2,3,4,5,6,7,8,9,0); // Mode two var arr2 = [1,2,3,4,5,6,7,8,9,0]; // Way Three var New Array (); arr3[0] = 1; arr3[1] = 2;
Multidimensional Arrays:
var arr = [ [[+], [4,5,6]];d ocument.write (arr[)); // 6
6.null,underfined
Null
In JavaScript, null means "nothing".
Null is a special type that has only one value, which represents an empty object reference.
You can set NULL to empty the object:
var person = null;
underfined
In JavaScript, undefined is a variable that has no value set.
typeof a variable that has no value returns undefined.
You can set the object to undefined to empty:
var person = undefined;
The difference between null and underfined:
typeof undefined // undefinedtypeofnull // ObjectNull = = = undefined // falsenull = = undefined // True
Iii. Viewing and conversion of data types
1. View data type typeof
var mood = "Happy"; alert (typeof mood); // outputs:stringalert (typeof ); // Outputs:number
2. Convert to String
var false ; alert (married.tostring ()); // outputs: "false"var = +; alert (age.tostring ()); //outputs: ""
3. Convert to Digital
Convert parseint () to integers
var test = parseint ("Blue"); // returns NaN var test = parseint ("1234blue"); // returns 1234 var // returns var // returns NaN
NaN: Numeric value that is not numeric
Parsefloat () converted to floating point number
var test = parsefloat ("1234blue"); // returns 1234 var test = parsefloat ("22.5"); // returns 22.5
Different types of operations:
var a = "all"; var b = 1; alert (a//231 Description: If it is not the same type, then + is the meaning of the stitching // Description: In addition to this special method of operation, even different types can be calculated
4. Determine the type of variable
A, Judge the string
typeof (a) = = "string";
B, judging the value
typeof (a) = = "Number";
C, if it is a numeric type, but the content is not a valid number, the Nan is displayed, and the Nan IsNaN (a) is judged
D, determine if the variable is empty (no variable is defined.) or define a variable but not initialize it will appear) typeof (a) = = "Undefined"
Iv. Operators
1. Arithmetic operators (+-*/%)
Alert ("10" +20); // return 1020; alert (10+20); // return; alert (20-10); // return ten; alert (10*5); // return; alert (10/5); Return 2;alert (10%5); // return 0;
2. Post Increment/decrement operator + +,--
var i = 0, = i++; // The first assignment after the Operation Console.log (j); // j=0
Pre-increment/decrement operator + +,--
var i = 0, = ++i; // The Console.log (j) is assigned after the first operation . // j=1
3. Comparison operator (<, >=, <=, = =,! =)
Alert (> 5); // outputs True var i = +; var n = += = n); // outputs Truealert (i! = n); // Outputs falsealert (i = = = N); // outputs True data type and value are equal
4. Logical operators (&& and, | | | Or! Non-
var i = 8; alert (i<5 && i<10); // Outputs falsealert (i > | | i < 10); // outputs Truealert (! ( > 5)); // outputs False
v. JavaScript annotations
Single-line Comment
/* ... * * Multi-line Comment
vi. Program Flow control
1. Conditional statement IF
Syntax: if (condition) Statements1 else statement2
Example:
var i = prompt ("Your score is:"); if (i >=) { alert ("excellent performance");} Else if (i >=) { alert ("score qualified");} Else { alert ("unqualified");}
2.switch statements
A variant of a conditional judgment, or another form.
Grammar:
Switch (expression) { case value: //statement break ; Case Value: // Statement Break ; default : // Statement}
Example:
vari = prompt ("Please select a set a,b,c,d");Switch(i) { CaseA: Alert ("You have ordered a package"); Break; CaseB: Alert ("You have ordered the B package"); Break; CaseC: Alert ("You have ordered C package"); Break; CaseD: Alert ("You have ordered D package"); Break; default: Alert ("Welcome to the next visit");}
JavaScript Basics Summary (ii)