当前位置:网站首页>2022.7.22 JS entry common data types and methods
2022.7.22 JS entry common data types and methods
2022-07-24 02:08:00 【The secret of longevity is sleep】
One 、script label
1. script Label writing
<!-- type You don't have to write , If you want to write, write it right -->
<script type="text/javascript"></script>
<!-- here type Deliberately write wrong in order to get inside h1 To replace the above to render -->
<script type="text/tpl">
<h1>{
{ name }}</h1>
</script>2.script Tags can be externally referenced
Two 、Number
1. Declaration definition :
// Literal declaration
var num = 1;
console.log(typeof num);
// Digital object mode declaration
var num = new Number(1);
console.log(num+4);
2. Digital method
Integer judgment
var num = 10
console.log(Number.isInteger(num)) //true
Specify the number of decimal places to return
var num = 5.2641654156
console.log(num.toFixed(2)) // Returns two decimal places , The result is 5.26
3.NaN Indicates a non numeric value ,isNaN() Judge whether it is a number
var num = '5'
console.log(Number(num))
console.log(isNaN(num))
// The output is 5 and false
null and '''' ( empty ) Will be judged as a number
4.js There will be errors in floating-point number calculation
var a = 0.1+0.2
console.log(a)
// The result is 0.30000000000000004. The solution is toFixed() Method
var b = 0.1+0.2
console.log(b.toFixed(1))
// The result is 0.3
3、 ... and 、String Common methods
// concatenation operator
var address = "xdclass.net",
name = " Xiaodi classroom ";
console.log(name + " The website is :" + address);
// To obtain the length of the
console.log("xdclass.net".length)
// toggle case
console.log('xdclass.net'.toUpperCase()); //XDCLASS.NET
console.log('XDCLASS.NET'.toLowerCase()); //xdclass.net
// Remove the blank
var str = ' xd class.net ';
console.log(str.length);
console.log(str.trim().length);
// Get single character
console.log('xdclass'.charAt(3)) //l
console.log('xdclass'[3]) //l
// Intercepting string
var n = 'xdclass'.slice(1,5);
console.log(n); //dcla
slice(start, end) //start( contain ),end( It doesn't contain )
// Find string
console.log('xdclass.net'.indexOf('s')); //5
console.log('xdclass.net'.indexOf('s', 6)); //6 From 6 Start searching with two characters ( Including 6 individual )
// Replace string
var name = " Xiaodi classroom ";
var changeName = name.replace(" Classroom ", " educational services ");
console.log(changeName); // Xiaodi educational institution
// Type conversion ( take "2022-01-22" The time format is converted to "2022/01/22")
var name = '2022-01-22';
console.log(name.split('-')); //['2022', '01', '22']
var a = name.split('-');
console.log(a.join('/')); //2022/01/22In type conversion ,split It takes a certain value as an interval to convert a string into an array ,join Then convert the interval point to the required value and return to the string form . In the above example '-' If replace with '' The following results will be produced
![]()
Four 、Boolean usage
1. Implicit conversion
// Almost all types can be implicitly converted to Boolean type
// Almost all types can be implicitly converted to Boolean type true false String Non empty string An empty string Number Not 0 The numerical 0/NaN Array When the array does not participate in the comparison Empty array for comparison Object undefined null NaNWhen other types are related to Boolean When comparing types , Other types will be converted to numeric types first and then compared .[] Converting an empty array to a number is 0 ,[1, 2] Non empty yes NaN .undefined and null The conversion to a number is NaN.
2. Explicit conversion
var xd = '' ;
console.log(!!xd); //false
xd = 0;
console.log(!!xd); //false
xd = null;
console.log(!!xd); //false
One ! Is to reverse the original value and convert it into Boolean , Two !! Is the original value Boolean value .
5、 ... and 、 Basic type data -Undefined、Null、Symbol
1.Undefined/Null These two are basically synonymous
undefined == null //true
//null Is a representation of “ nothing ” The object of , The conversion to a value is 0 ;undefined Is a representation of “ nothing ” Original value , When it is converted to a number, it is NaN.
console.log(Number(null)) //0
console.log(Number(undefined)) //NaN
console.log(Number(1+null)) //1
console.log(Number(1+undefined)) //NaN
2.Undefined
// Variables are declared but not assigned , Is equal to undefined .
var i ; // i undefined
// Object has no assigned property , The value is undefined .
var a = Object(); //a.name undefined
// Function has no return value default return undefined
var x = f(); //x undefined
3.Null
null Express “ There is no object ”, That is, there should be no value .
usage :
(1) As an argument to a function , The parameter representing the function is not an object
(2) As the end of the object prototype chain
Object.getPrototypeOf(Object.prototype) // null
4.Symbol(es6 The grammar of ) Generate unique attributes
// You need to add the same attribute name to an object
var xd = { aa: '1' };
var aa = Symbol('aa');
xd[aa] = '2';
console.log(xd);
边栏推荐
- [untitled]
- 架构实战营模块二作业
- 毕业设计校园信息发布平台网站源码
- Halide::Generator生成器使用说明
- hdu-7141 Ball (bitset)
- The difference between.Split (",", -1) and.Split (",")
- Decrypt redis to help the e-commerce seckill system behind the double 11
- Design of hospital wireless network system
- Phpcms realizes product multi condition screening function
- Database paradigm and schema decomposition
猜你喜欢

Yinshimei Invisalign oral scan referral method (export oral scan data + online consultation)

Construction and test of hfish honey pot

Try to run this command from the system terminal Make sure that you use the correct

Summary of the first change to open source middleware keycloak

jenkins多任务并发构建

145-keep-alive的初步使用

Exchange 2013 SSL certificate installation document

医院网络安全架构

Seatunnel architecture

Structure the second operation of the actual combat battalion module
随机推荐
浅谈元宇宙中DeFi的可能性和局限性
Exchange 2013 SSL证书安装文档
141. Circular linked list
Performance test of ArrayList and LinkedList insertion based on jmh
Spark memory management mechanism new version
Distributed resource management and task scheduling framework yarn
使用第三方账号登录
Construction and test of hfish honey pot
Halide::Generator生成器使用说明
Phpcms realizes product multi condition screening function
Quick sort considerations
毕业设计校园信息发布平台网站源码
Hospital generic cabling
杂志特稿:元宇宙将重塑我们的生活,我们要确保它变得更好
Magazine feature: the metauniverse will reshape our lives, and we need to make sure it gets better
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
xxl-job使用注意事项
Study and use of burpsuite plug-in
Digicert code signing certificate
Installation, configuration and use of sentry