Home >Web Front-end >JS Tutorial >Getting Started with JavaScript Programming (Lesson 2)
Today we mainly study the following content:
1. Java script variables
2. Java script expressions and operators
can be added as variables with var Declares a variable with a specified name, and the variable type can be determined by assigning a value to the variable. Since JavaScript uses a weakly typed style, the requirements for data types are not too strict. During the execution of the program, it will be automatically converted as needed. For string variables, you can get the length of the string in the variable through "variable name.length", such as
var name;
name="java script";
Then the value of name.length is 10.
If you create multiple variables in one line, remember to separate the variable names with commas. Separate each statement with a semicolon. (Using semicolons is a good habit, everyone should try to develop the habit of adding semicolons when learning)
Type conversion: javascript allows changing the type of variables in the program, the most common two Type conversion characters Number and String. Number(x) is a character value——>numeric value type. String is the opposite. Compared to JavaScript's automatic type conversion, this conversion can be called forced type conversion. (Forced type conversion can only be used in javascript1.2 and above)
Variable naming:
1. It must start with a letter or an underscore, and it can be in the middle Alphanumeric and or underlined. Spaces, , - and other symbols cannot be used. Except for hyphens, variable names cannot have spaces, (+), (-), (,) or other symbols.
2. You cannot use keywords in JavaScript as variables. (Java script variable names are case-sensitive, name and Name are different.)
There is another importance for variables-that is the scope of the variable. There are also global variables and local variables in JavaScript. Global variables are defined outside all function bodies, and their scope is the entire function; local variables are defined within the function body, and are only visible to the function, but not to other functions.
For more related tutorials, please visit JavaScript Video Tutorial