type

What are the language types of JavaScript?

JavaScript has seven language types:

Language type Result of typeof operation
Basic types of object
null object
undefined undefined
boolean boolean
number number
string string
symbol symbol
object object

How to correctly detect null value types?

let a = null; ! a &&typeof a === 'object';
Copy the code

What is the difference between undefined and null?

Undefined means undefined. We can specify a custom variable as undefined by using the global variable undefined. Note that undefined is an identifier and not a JavaScript keyword, so it can be used and assigned as a variable. Null means defined but null. Null is a JavaScript keyword, and the engine will throw an error when you try to assign to it.

Why are strings immutable?

String immutability means that a string member method does not change its original value, but creates and returns a new string.

0.1 + 0.2 === 0.3?

From a normal mathematical point of view the answer is clearly true, but in fact 0.1 + 0.2 = 0.30000000000000004 in JavaScript, so the result of the above question is false.

How do I compare whether two floating point numbers are equal?

For JavaScript, the margin of error for numeric comparisons is 2^-52, and when the result of subtracting two numbers is less than the margin of error, the two floating-point numbers are considered equal.

const a = 0.1 + 0.2;
const b = 0.3;

function numbersCloseEnoughToEqual(n1, n2) {
  return Math.abs(n1 - n2) < Math.pow(2, -52);
}

numbersCloseEnoughToEqual(a, b); // true;
Copy the code

How do I tell if a variable of type number is a NaN?

To start with ES6 you can use number.isnan (…) To determine if a variable is a NaN, do not use window.isnan (…). Because the built-in global function is flawed. When we do not support the ES6 syntax, we can set it to polyfill:

if (!Number.isNaN) {
  Number.isNaN = function(n) {
    returnn ! == n;// NaN is the only value in JavaScript that does not equal itself
  };
};
Copy the code

+ 0 = = = 0?

In JavaScript -0 exists, and +0 === -0 results in true, and when we convert -0 to a string we get “0”. If you need to check for -0, you can do this:

function isNegZero(n) {
  return n === 0 && 1 / n === -Infinity;
}
Copy the code

How can I safely check variable types?

We can solve this problem by combining two knowledge points:

  1. When a method is called, JavaScript wraps it as a wrapped object.
  2. Built-in properties [[Class]]The built-in property describes what type of value a variable holds that we can passObject.prototype.toString(...)To access the property.
var a = 'abc';
Object.prototype.toString.call(a); // [object String]

var b = null;
Object.prototype.toString.call(a); // [object Null]
Copy the code

Cast casting

What is a cast, and what rules does it follow?

From a language perspective, the conversion of a value from one type to another is called a cast, while the implicit case is called a cast, and a cast always returns a scalar primitive type. Here are three conversion rules for abstract operations: ToString:

Boolean Number Null Undefined
String ‘true’ and ‘false’ Follow the general rules (maximum and minimum values will be exponential) ‘null’ ‘undefined’

ToNumber:

Boolean String Null Undefined
Number true -> 1 false -> 0 NaN is returned on processing failure, following the rules for numeric constants 0 NaN

ToBoolean: Undefined, null, false, +0, -0, NaN, ” ” the seven false values will get false when cast, and will be converted to true for all values outside the list of false values.

parseInt(…) Is the operation a cast? Do you know the result of the following question?

var a = '12a';
var b = parseInt(a, 10); / / 12
var c = Number(a); // NaN
Copy the code

Answer, parseInt(…) Operations are generally thought of as parsing rather than casting. As you can see from the above problem, parsing allows non-numeric characters, while casting does not, and will return NaN.

What is the result of ToString abstraction on a normal array [1,2,3]?

When ToString abstraction is performed on a normal array, the engine casts all the elements, making them all strings, and then concatenates them.

var arr = [1.2.3];
arr.toString(); / / 1, 2, 3
Copy the code

You really know json.stringify (…) What is the result of converting the following objects?

var o = {
  name: 'O_c'.character: ['open'.undefined.'sanguine'].getName: function() {
    return this.name; },Symbol.toStringTag]: 'myObject'
};

JSON.stringify(o); // "{"name":"O_c","character":["open",null,"sanguine"]}"
Copy the code

JSON. Stringify will ignore undefined, function, and symbol when handling objects, and the JavaScript engine will replace them with null when they are in the array. To keep the unit in the same position.

Is there a difference between a + “” and String(a)?

In most cases, a + “” is simply interpreted as converting the value of a to a String, which doesn’t look very different from String(a), but run the following example:

var o = {
  valueOf() {
  	return 10;
  },
  toString() {
  	return 1; }}; o +"";
String(o);
Copy the code

Obviously o + “” results in 10, while String(o) results in 1. In fact, when one of the + operands is a String, it implicitly casts the other operands. The ToPrimitive abstraction is performed when an object is encountered, first executing the valueOf() method and then converting valueOf’s return value to a String through the ToString abstraction, while String() calls the ToString abstraction directly.

Why do we always avoid loose equality ==,"42" == trueThe result?

In response to the above questions, our brain will subconsciously consider whether “42” is a true value or a false value. Obviously it is a true value, so we would assume that the result of the above question is true, when in fact it is false. In a loose equality comparison, when one of the operands is a Boolean, the ToNumber abstraction is performed on the Boolean, both when an implicit cast occurs. So “42” == 1 results in false. Other types of implicit cast mechanisms are as follows:

  • A ToNumber abstraction is performed on a String variable when a comparison is made between a String and a number.
  • Boolean variables are ToNumber abstracted when compared between other types and Booleans.
  • In loose equality, null and undefined are equal.
  • ToPrimitive abstractions are performed on variables of type Object when comparing objects with non-objects.
  • Object to object comparisons are equal only if the two objects point to the same address, and no casts occur.