iT邦幫忙

2023 iThome 鐵人賽

DAY 3
0
Modern Web

前端知識系列 第 3

JavaScript Data Types Interview Preparation

  • 分享至 

  • xImage
  •  

來源

Primitive Types

Question

Can you list all the primitive types in JavaScript and briefly explain each one?

Original Answer

there are seven, string number boolean undefiined null Bignum Symbol

Feedback

You correctly listed all seven primitive types, but the answer could be more organized. Also, it's "BigInt," not "Bignum."

Corrected Answer

There are seven primitive types in JavaScript: String, Number, Boolean, Undefined, Null, BigInt, and Symbol.


Immutability

Question

The article mentions that primitive types are immutable. What does that mean, and can you provide an example?

Original Answer

when we concat a string, it will not adjust the originvaleu,instead it wiill retuern a new stering

Feedback

Your explanation is correct but could be clearer.

Corrected Answer

Primitive types are immutable in JavaScript. For example, when you concatenate two strings, a new string is returned, and the original strings remain unchanged.


Number Type

Question

JavaScript doesn't differentiate between integers and floating-point numbers. Can you explain how JavaScript handles numbers and any limitations it might have?

Original Answer

js is dloble-float preicsing , and whem it over limiet it will have preciscsion isssue, and infinty -infinty and NaN is also number

Feedback

You mentioned double-precision floating-point numbers, which is good. However, the explanation could be more coherent.

Corrected Answer

JavaScript uses double-precision floating-point numbers. This format has limitations on precision. Special values like +Infinity, -Infinity, and NaN are also considered of type Number.


BigInt

Question

What is BigInt, and when would you use it instead of the standard number type?

Original Answer

whem the value is bigger or smaller than the number type can handle , i will use bigint, but need to careful let numer and bigint can not be changealbe

Feedback

You got the main point about when to use BigInt, but the explanation could be clearer.

Corrected Answer

BigInt is used when you need to handle numbers that are outside the safe integer range of standard JavaScript numbers. However, it's important to note that BigInt and Number types are not interchangeable.


Undefined vs Null

Question

Both undefined and null are primitive types in JavaScript. Can you explain the difference between them?

Original Answer

undefinednf mean acitiluu dont know the value, and null mean i know there is no value

Feedback

Your explanation is correct but could be more precise.

Corrected Answer

The term undefined means that a variable has been declared but not assigned a value. On the other hand, null means that the variable has been explicitly set to have no value.


Symbol

Question

What is a Symbol in JavaScript, and what are some use-cases for it?

Original Answer

it can prooduce a uniqued value, and usimg use for object key, especillt for buildomg libioray ,it cna prevent other useing the same object key to cause overide the orignal value

Feedback

Good explanation! Symbols are indeed unique and can be used as object keys to prevent accidental overwrites.

Corrected Answer

Symbols are unique and can be used as object keys. This is especially useful for building libraries, as it prevents accidental overwrites of object keys.


Typeof Operator

Question

How do you identify the type of a variable in JavaScript? Are there any quirks or limitations to be aware of when using typeof?

Original Answer

use typeof, when use typeof to null it will return object and it is a bug that js dont want to fix bzv it ost to high,but when we deal wiith funtion and array , we can uer Array.isarrayy to idnentify is it a array, and alse we can use object.prototype.tostring to indetfiy its type

Feedback

You covered the main points, including the bug with null. However, the explanation could be more organized.

Corrected Answer

The typeof operator is commonly used to identify the type of a variable. However, it has a known bug where typeof null returns 'object'. To identify arrays, you can use Array.isArray(), and for more complex type checking, Object.prototype.toString.call() can be used.


Array Identification

Question

How would you distinguish between an array and an object in JavaScript?

Original Answer

use Array.isarray

Feedback

Correct, Array.isArray() is used to identify arrays.

Corrected Answer

To distinguish between an array and an object, you can use Array.isArray().


Object Types

Question

The article mentions that arrays and functions are also objects in JavaScript. Can you elaborate on that?

Original Answer

when we use typeof array and finition it will retuen object

Feedback

You're correct that typeof returns 'object' for arrays and functions, but it would be good to elaborate on why this is the case.

Corrected Answer

When you use typeof on an array or a function, it returns 'object'. This is because, in JavaScript, arrays and functions are actually objects.


Historical Bug

Question

The article mentions a historical bug related to the typeof operator and null. Can you explain what that bug is?

Original Answer

it will return object but acrully its type is null

Feedback

You correctly identified the bug with typeof null returning 'object'.

Corrected Answer

There is a historical bug in JavaScript where typeof null returns 'object', even though its actual type should be null.---


上一篇
2023/09/17
下一篇
JavaScript Equality Interview Preparation
系列文
前端知識30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言