Home > Java > javaTutorial > body text

Front-end interview questions - JavaScript data type conversion - JavaScript ING

不言
Release: 2018-03-30 10:00:42
Original
2353 people have browsed it

This article shares with you about some front-end interview questions, hoping to help friends in need

1. Dynamic type language

JavaScript is a kind of dynamic Typed language (dynamically typed language). This means that you don't have to specify a data type when declaring a variable, and the data type will be automatically converted as needed when the script is executed. 1.1 Define variables

Front-end interview questions - JavaScript data type conversion - JavaScript INGAnd you can also assign a string value to the same variable, such as

Front-end interview questions - JavaScript data type conversion - JavaScript INGBecause JavaScript is dynamically typed, assignment

will not prompt an error

. 1.2 Addition operator (+)

In numeric and string expressions involving the addition operator (+), JavaScript will convert the

numeric value into a string

. For example

Front-end interview questions - JavaScript data type conversion - JavaScript ING But when it comes to other operators (Annotation: such as the minus sign '

-

' below), the JavaScript language Does not convert numbers into strings. For example (Annotation: The first example is mathematical operation, the second example is string operation):

Front-end interview questions - JavaScript data type conversion - JavaScript ING 2. Typeof operator

Since variables in JavaScript are

loosely typed

, JavaScript provides an operator to detect the data type of the current variable, which is typeof. By using typeof()

Get the data type of the object

, including the following types: boolean, string, number, undefined, object, function. 2.1 boolean ------------- Boolean value Boolean

Front-end interview questions - JavaScript data type conversion - JavaScript ING2.2 string --- -------------- String String

Front-end interview questions - JavaScript data type conversion - JavaScript ING#2.3 number ---------- ---- Numeric Number

Front-end interview questions - JavaScript data type conversion - JavaScript ING2.4 undefined ---------- UndefinedUndefined

Front-end interview questions - JavaScript data type conversion - JavaScript ING2.5 object ---------------- Object or null Object

Front-end interview questions - JavaScript data type conversion - JavaScript ING2.6 function --------------- Function Function

Front-end interview questions - JavaScript data type conversion - JavaScript ING##Note

(1) The data type of

NaN

is number

(2) The data type of undefined variable

is undefined

(3) The data type of array

(Array) is object

(4)The data type of date

(Date) is object

(5)## The data type of #null is object

(6) The data type of function (function) is function

3. Convert string to number 3.1 Number() function

3.2 parseInt() function

3.3 parseFloat() function

3.4 Unary addition operator

4. Number() function

4.1 Definition and usage

Number() function converts the value of an object into a number.

4.2 Syntax

Number(object)

ParametersDescriptionobjectRequired. JavaScript object.

4.3 Return value

(1) If the parameter is a Date object, Number() returns the number of milliseconds from January 1, 1970 to the present.

(2) If the value of the object cannot be converted to a number , then the Number() function returns NaN.

4.4 Example

Front-end interview questions - JavaScript data type conversion - JavaScript ING

5. parseInt() function

5.1 Definition and usage

## The #parseInt() function parses a string and returns an
integer.
(1) When the value of parameter radix is ​​

0, or the parameter is not set, parseInt() will determine the base of the number based on string.

(2) When

ignores the parameter radix, JavaScript defaults the radix of the number as follows:

  • If the string starts with "

    0x " at the beginning, parseInt() will parse the rest of the string into a hexadecimal integer.

  • If string starts with

    0, then ECMAScript v3 allows an implementation of parseInt() to parse the following characters into octal or hexadecimal number.

  • If string starts with a number from

    1 ~ 9, parseInt() will parse it into a decimal integer.

5.2 Syntax

parseInt(string, radix)
##Parametersstringradix5.3 Tips and Notes
Description
Required. The string to be parsed.
Optional. Represents the base of the number to be parsed. The value is between 2 ~ 36.

(1) Only the

first number

in the string will be returned. (2) Spaces at the beginning of

and at the end of

are allowed. (3) If the

first character of the string cannot be converted to a number

, then parseInt() will return NaN. (4) Old browsers use octal base by default when the string starts with "0". ECMAScript 5, the default is decimal base.

5.4 Example

Front-end interview questions - JavaScript data type conversion - JavaScript ING6. parseFloat() function

6.1 Definition and usage

## The #parseFloat() function parses a string and returns a

float

.
This function specifies whether the first character in the string is a number
. If so, the string is parsed until the end of the number is reached, and the number is returned as

number rather than as a string. 6.2 Syntax

parseFloat(string)

##ParametersDescriptionstringRequired. The string to be parsed.

6.3 Tips and comments

(1) Only the first number is returned in the string.

(2) Spaces at the beginning of and at the end of are allowed.

(3) If the first character of the string cannot be converted to a number , then parseFloat() will return NaN.

6.4 Example

Front-end interview questions - JavaScript data type conversion - JavaScript ING

7. Unary addition operator

Front-end interview questions - JavaScript data type conversion - JavaScript ING

8. Convert numbers to strings

To be continued


##1. Dynamic type language

JavaScript It is a dynamically typed language. This means that you don't have to specify a data type when declaring a variable, and the data type will be automatically converted as needed when the script is executed.
1.1 Define variables


##And you can also assign a character to the same variable String value, such as Front-end interview questions - JavaScript data type conversion - JavaScript ING

Because JavaScript is dynamically typed, such assignment Front-end interview questions - JavaScript data type conversion - JavaScript ING will not prompt an error .

1.2 Addition operator (+)In numeric and string expressions involving the addition operator (+), JavaScript will convert the

numeric value into a string

. For example

But when it comes to other operators (Annotation: such as the minus sign 'Front-end interview questions - JavaScript data type conversion - JavaScript ING-' below), the JavaScript language Does not convert numbers into strings. For example (Annotation: The first example is

mathematical operation

, the second example is string operation):

2. Typeof operatorFront-end interview questions - JavaScript data type conversion - JavaScript INGSince variables in JavaScript are

loosely typed

, JavaScript provides an operator to detect the data type of the current variable, which is typeof.

By using typeof()Get the data type of the object, including the following types: boolean, string, number, undefined, object, function.

2.1 boolean ------------- Boolean value Boolean

2.2 string --- -------------- String StringFront-end interview questions - JavaScript data type conversion - JavaScript ING

#2.3 number ---------- ---- Numeric NumberFront-end interview questions - JavaScript data type conversion - JavaScript ING

2.4 undefined ---------- UndefinedUndefinedFront-end interview questions - JavaScript data type conversion - JavaScript ING

2.5 object ---------------- Object or null ObjectFront-end interview questions - JavaScript data type conversion - JavaScript ING

2.6 function --------------- Function FunctionFront-end interview questions - JavaScript data type conversion - JavaScript ING

##Note Front-end interview questions - JavaScript data type conversion - JavaScript ING (1) The data type of NaN

is number

(2)

The data type of undefined variable is undefined

(3)

The data type of array(Array) is object

(4)

The data type of date(Date) is object

(5)## The data type of #null

is object (6)

The data type of function

(function) is function 3. Convert string to number

3.1 Number() function3.2 parseInt() function

3.3 parseFloat() function

3.4 Unary addition operator

4. Number() function

4.1 Definition and usage

Number() function converts the value of an object into a number.

4.2 Syntax

Number(object)

Parameters
DescriptionRequired. JavaScript object.
object

4.3 Return value

(1) If the parameter is a Date object, Number() returns the number of milliseconds from January 1, 1970 to the present.

(2) If the value of the object cannot be converted to a number , then the Number() function returns NaN.

4.4 Example

Front-end interview questions - JavaScript data type conversion - JavaScript ING

5. parseInt() function

5.1 Definition and usage

## The #parseInt() function parses a string and returns an
integer.
(1) When the value of parameter radix is ​​

0, or the parameter is not set, parseInt() will determine the base of the number based on string.

(2) When

ignores the parameter radix, JavaScript defaults the radix of the number as follows:

  • If the string starts with "

    0x " at the beginning, parseInt() will parse the rest of the string into a hexadecimal integer.

  • If string starts with

    0, then ECMAScript v3 allows an implementation of parseInt() to parse the following characters into octal or hexadecimal number.

  • If string starts with a number from

    1 ~ 9, parseInt() will parse it into a decimal integer.

5.2 Syntax

parseInt(string, radix)
##Parametersstringradix5.3 Tips and Notes
Description
Required. The string to be parsed.
Optional. Represents the base of the number to be parsed. The value is between 2 ~ 36.

(1) Only the

first number

in the string will be returned. (2) Spaces at the beginning of

and at the end of

are allowed. (3) If the

first character of the string cannot be converted to a number

, then parseInt() will return NaN. (4) Old browsers use octal base by default when the string starts with "0". ECMAScript 5, the default is decimal base.

5.4 Example

Front-end interview questions - JavaScript data type conversion - JavaScript ING6. parseFloat() function

6.1 Definition and usage

## The #parseFloat() function parses a string and returns a

float

.
This function specifies whether the first character in the string is a number
. If so, the string is parsed until the end of the number is reached, and the number is returned as

number rather than as a string. 6.2 Syntax

parseFloat(string)

##ParametersDescriptionstringRequired. The string to be parsed.

6.3 提示和注释

(1)字符串中只返回第一个数字

(2)开头和结尾的空格是允许的。

(3)如果字符串的第一个字符不能被转换为数字,那么 parseFloat() 会返回 NaN

6.4 实例

Front-end interview questions - JavaScript data type conversion - JavaScript ING

七、单目加法运算符

Front-end interview questions - JavaScript data type conversion - JavaScript ING

八、数字转换为字符串

未完待续


相关推荐:

2018前端面试常见算法题

前端面试题小结





                 


                                              


The above is the detailed content of Front-end interview questions - JavaScript data type conversion - JavaScript ING. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!