Home  >  Article  >  Web Front-end  >  Detailed explanation of js basic packaging types

Detailed explanation of js basic packaging types

小云云
小云云Original
2018-03-27 17:30:121604browse

This article mainly shares with you the detailed explanation of the basic packaging types of js. I hope it can help you.

1. Boolean type

var falseObject = new Boolean(false);
//falseObject是对象
var result = falseObject && true;alert(result); 
//truevar falseValue = false;
//基本类型,booleanresult = falseValue && true;alert(result); //false

All objects in the Boolean expression will be converted to true, so the falseObject object represents true in the Boolean expression. As a result, true && true is of course equal to true.

2. Number type

The Number type also provides some methods for formatting values ​​into strings

Among them, toFixed() The method will return the string representation of the value according to the specified decimal places, for example:

var num = 10.005;
alert(num.toFixed(2)); //"10.01"

Exponential representation:

var num = 10;

alert(num.toExponential(1)); //"1.0e+1"

3. Character operation method

charAt():返回指定位置的值;
charCodeAt():返回指定位置值的字符编码;
concat()方法,括号中可以存在多个参数
slice、substring和substr都不会改变字符串本身的值
var stringValue = "hello world";alert(stringValue.slice(3)); 
//"lo world"alert(stringValue.substring(3)); //"lo world"alert(stringValue.substr(3)); //"lo world"alert(stringValue.slice(3, 7)); //"lo w"alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl" //第二个参数指字符串的长度

Slice() and substr() behave the same when passed a negative
argument. This is because -3 will be converted to 8 (string length plus parameters 11+(3)=8), which is actually equivalent to calling slice(8) and substr(8). But the substring() method returns the entire string because it converts -3 into 0.

The three methods behave differently when the second parameter is a negative value. The slice() method will convert the second parameter to 11+(-4)=7, which is equivalent to calling slice(3,7), so "low" is returned. The substring() method will convert the second parameter to 0, making the call

substring(3,0). Since this method will use the smaller number as the starting position and the larger number as End position,

So it is ultimately equivalent to calling substring(0,3). substr() also converts the second argument to 0, which means that a string containing zero characters is returned, which is an empty string.

Related recommendations:


Analysis of basic packaging types in JavaScript

The above is the detailed content of Detailed explanation of js basic packaging types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn