ArticleDirectory
- Preface
- Javascript Variables
- Data Types of variables in Javascript
- Conversions between types
- Operations between various types of Variables
Back to directory
Preface
The success of the Javascript scripting language is largely reflected in its compatibility with major browsers. All major browsers follow the ecmascript standard, this also makes JavaScript a necessary chip for front-end engineering. Your JS bull may have a 5-digit salary!
Some people say that JS is a scripting language, one method, one small plug-in, but I don't think so. For any development language, it can be object-oriented, you can follow the object-oriented principle. Only in this way can you developCodeIt is robust, readable, and comfortable to look.
Javascript Variables
Variables in javascript can be divided into global variables and local variables. When you do not declare var, it is declared as a global variable by default, and VaR is the local variable in the current method, for a global variable, its scope is the current page. it is visible to all <SCRIPT> blocks on the current page, and its scope for a local variable is the current method block. Definition:
<SCRIPT>Variableglobal=1; // Global
VaRVariableglobal2 = "1"; // variables defined outside all function methods, declared with VAR, are global
Function Sub (){
VaR A = "1" // local variable
}</SCRIPT> <SCRIPT>Alert (variableglobal)</SCRIPT>
Data Types of variables in Javascript
The data types in development languages are similar. For JS, they include integer, floating point, Boolean, data type, JSON type, and object type. They can be computed accordingly, for example, between an integer and an integer, between an integer and a floating point, and between an integer and a floating point.
Several common types and how to define their Variables
// Note: javascript is case sensitive. VaR Variableint = 1 ; // Integer VaR Variablestring = " 1 " ; // Variable type VaR Variablebool = True ; // Boolean variable VaR Variablefloat = 1.01 ; // Floating-point variables (in some languages, they are called Single-precision variables) VaR Variablearray = [ 1 , 2 , 3 , " One " , " Two " , " Three " ]; // Array Variables VaR Variablejson = [{ID: " 1 " , Name: " Zzl " }, {ID: " 2 " , Name: " Ninali " }]; // JSON Object Type Variable VaR Variableobject = {ID:1 , Name: " Zzl " , Age: 30 , Ismarry: True }; // JS object variables
Conversions between types
VaRStringtoint =Parseint (variablestring );VaRStrinttofloat =Parsefloat (variablestring); console. Log (stringtoint); console. Log (strinttofloat );
Operations between various types of Variables
Console. Log (variableint + variableint );// Integer calculation Console. Log (variableint + variablestring ); // Integer and balanced computing Console. Log (variableint + variablefloat ); // Integer and precision computing Console. Log (variablebool); console. Log ( ! Variablebool ); // Logical non-operation on the Boolean Type Console. Log (variablebool && False ); // Logic and operation on the Boolean Type Console. Log (variablebool | False ); // Boolean logic or operation For ( VaR I = 0 , Max = variablearray. length; I <Max; I ++ ){ // Array Traversal Console. Log (variablearray [I]);} For ( VaR I = 0 , Max = variablejson. length; I <Max; I ++ ){// Traversal of JSON objects Console. Log ( " Id index: " + Variablejson [I] [ " ID " ] + " The corresponding name is: " + Variablejson [I] [ " Name " ]);} Console. Log ( " In JS objects, the ID attribute is " + Variableobject. ID + " The name attribute is " + Variableobject. Name) // Output JS object Value
OK. Let's take a look at the introduction of variables. You can write your own code!
Thank you for reading!
Back to directory