This article brings you relevant knowledge about javascript. It mainly introduces JavaScript variable types and conversions between variables in detail. The sample code in the article is introduced in great detail and has certain Reference value, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end】
1. Variable type
<script>
var num =10;
console.log(typeof num);
</script>
Copy after login
typeof
: Declaration of variable type
##alert: Pop-up alert box
prompt: Input and output statements
1.1 Color of variable type
Identify variable type according to the color of the console :
- Purple: Numeric type
- Black: Character type
- Blue: Boolean type (
true false)
- Light gray:
undefined null
2. Literal
Literal is the representation of a fixed value in the source code, that is, the literal represents how to express this value.
3. Data type conversion
3.1. Convert to string type
1. Add sign to concatenate string
console.log(num 'I am a string')
2.toString() Convert to a string
The string representation of a number. For example, when radix is 2, the NumberObject is converted to a string representing the binary value.
The syntax is:
number.toString(radix)
Copy after login
Parameter Description
radix Optional. Specifies the base of the number, which is an integer between 2 and 36. If this parameter is omitted, base 10 is used. Note, however, that the ECMAScript standard allows implementations to return any value if the parameter is a value other than 10.
- 2 - The number is displayed as a binary value
- 8 - The number is displayed as an octal value
- 16 - The number is displayed as a hexadecimal value
Example is as follows:
var num=10;
var str=num.toString();
console.log(str);
Copy after login
3.string (variable)
3.2 Convert to numeric type (key point)
parseInt(string) function - integer numerical type
parseInt() function can parse a character String and returns an integer.
When the value of parameter radix is 0, or the parameter is not set, parseInt() will determine the base of the number based on string.
When the parameter radix is omitted, JavaScript defaults to the radix of numbers as follows:
If string starts with "0x", parseInt() will parse the rest of the string into hexadecimal integers.
If string begins with 0, then ECMAScript v3 allows an implementation of parseInt() to parse subsequent characters into octal or hexadecimal digits.
If string starts with a number from 1 to 9, parseInt() will parse it into a decimal integer.
Grammar
parseInt(string, radix)
Copy after login
The example is as follows
var age=prompt('请输入您的年龄');
console .log(parseInt(age));
console.log(parseInt('3.14')); //取整 3
console.log(parseInt('3.98')); //取整 3
console.log(parseInt('120px')); //取整 120
Copy after login
2.parseFloat(string) function--floating point numerical type
parseFloat( ) function parses a string and returns a floating point number.
This function specifies whether the first character in the string is a number. If it is, the string is parsed until it reaches the end of the number, and the number is returned as a number rather than as a string.
Grammar
parseFloat(string)
Copy after login
The example is as follows
console.log(parseFloat('3.14'));
console.log(parseFloat('120px')); //120会去掉px单位
Copy after login
3.Use number (variable)var str='123';
console.log(number(str);
console.log(number('123'));
Copy after login
4.Use Arithmetic operations - * / Implicit conversionconsole.log('12'-0); //12
console.log('123'-'120'); //3
console.log('123'*1); //123
Copy after login
3.3 Convert to Boolean
Boolean() function
Boolean object represents two Value: "true" or "false"
The following code defines a Boolean object named myBoolean:
var myBoolean=new Boolean();
Copy after login
If the Boolean object has no initial value or its value is:
- 0
- -0
- null
- ""
- false
- undefined
- NaN
Then the value of the object is false. Otherwise, its value is true (even when the variable value is the string "false")!
console.log(boolean(''));//false
console.log(boolean(0));//false
console.log(boolean(NaN));//false
console.log(boolean(null));//false
console.log(boolean(undefined));//false
Copy after login
The rest are true.
【Related recommendations:
javascript video tutorial, web front-end】
The above is the detailed content of Take you to understand JavaScript variable types and conversion between variables. For more information, please follow other related articles on the PHP Chinese website!