当前位置:网站首页>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);
边栏推荐
- Number of combinations....
- Spark partition operators partitionby, coalesce, repartition
- Jenkins multitâche construction simultanée
- [pumpkin Book ml] (task3) decision tree (updating)
- Install SSL Certificate in Litespeed web server
- 文心大模型扬起新“帆”,产业应用大潮已至
- Phpcms realizes product multi condition screening function
- Seatunnel architecture
- 通过Arduino IDE向闪存文件系统上传文件
- C - structure
猜你喜欢

College degree want to 0 basic programming after looking for a job feasible?

5年接触近百位老板,身为猎头的我,发现升职的秘密不过4个字

Distributed resource management and task scheduling framework yarn

Cinq ans de contact avec près d'une centaine de patrons, en tant que chasseur de têtes, j'a i découvert que le secret de la promotion n'est que quatre mots

深入了解-微信开发者工具

Install SSL Certificate in Litespeed web server

Database paradigm and schema decomposition

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

ASP.NET CORE写一个缓存Attribute工具

杂志特稿:元宇宙将重塑我们的生活,我们要确保它变得更好
随机推荐
Wenxin big model raises a new "sail", and the tide of industrial application has arrived
canvas-绘图(鼠标按下 绘制 抬起 结束)
Thread pool interview
Computer room construction data
Exchange 2010 wildcard SSL certificate installation document
Design of hospital wireless network system
Is software testing still popular in 2022?
[重要通知]星球线上培训第三期来袭!讲解如何在QTYX上构建自己的量化策略!...
什么叫裸写SQL?express操作mysql用什么中件间或插件好呢?
[pumpkin Book ml] (task3) decision tree (updating)
Three document usage
Structure the second operation of the actual combat battalion module
LiteSpeed Web服务器中安装SSL证书
浅谈领域驱动设计
解决script标签写在元素节点前面无法获取元素节点的问题
2022-07-22: what is the output of the following go language code? A:1; B:1.5; C: Compilation error; D:1.49。 package main import “fmt“ func main() { var i
How CAD draws arrows with arcs
Halide::Generator生成器使用说明
C - structure
Database security and data integrity