Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Object-Oriented JavaScript
  • Table Of Contents Toc
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)
close
close
Object-Oriented JavaScript

Object-Oriented JavaScript

4.5 (48)

Overview of this book

Table of Contents (18 chapters)
close
close
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
2
Built-in Functions
chevron up
4
Regular Expressions
5
Index

Appendix B. Built-in Functions

This Appendix contains a list of the built-in functions (methods of the global object), discussed in Chapter 3.

Function

Description

parseInt()

Takes two parameters: an input object and radix; then tries to return an integer representation of the input. Doesn't handle exponents in the input. The default radix is 10 (a decimal number). Returns NaN on failure. Omitting the radix may lead to unexpected results (for example for inputs such as 08), so it's best to always specify it.

>>> parseInt('10e+3')

10

>>> parseInt('FF')

NaN

>>> parseInt('FF', 16)

255

parseFloat()

Takes a parameter and tries to return a floating-point number representation of it. Understands exponents in the input.

>>> parseFloat('10e+3')

10000

>>> parseFloat('123.456test')

123.456

isNaN()

Abbreviated from "Is Not a Number". Accepts a parameter and returns true if the parameter is not a valid number, false otherwise. Attempts to convert the input to a number first.

>>> isNaN(NaN)

true

>>> isNaN(123)

false

>>> isNaN(parseInt('FF'))

true

>>> isNaN(parseInt('FF', 16))

false

isFinite()

Returns true if the input is a number (or can be converted to a number), but is not the number Infinity or -Infinity. Returns false for infinity or non-numeric values.

>>> isFinite(1e+1000)

false

>>> isFinite(-Infinity)

false

>>> isFinite("123")

true

encodeURIComponent()

Converts the input into a URL-encoded string. For more details on how URL encoding works, consult the Wikipedia article: http://en.wikipedia.org/wiki/Url_encode

>>>encodeURIComponent('http://phpied.com/')

"http%3A%2F%2Fphpied.com%2F"

>>> encodeURIComponent('some script?key=v@lue')

"some%20script%3Fkey%3Dv%40lue"

decodeURIComponent()

Takes an URL-encoded string and decodes it.

>>> decodeURIComponent('%20%40')

" @"

encodeURI()

URL-encodes the input, but assumes a full URL is given, so returns a valid URL by not encoding the protocol (for example http://) and host name (for example www.phpied.com).

>>> encodeURI('http://phpied.com/')

"http://phpied.com/"

>>> encodeURI('some script?key=v@lue')

"some%20script?key=v@lue"

decodeURI()

Opposite of encodeURI().

>>> decodeURI("some%20script?key=v@lue")

"some script?key=v@lue"

eval()

Accepts a string of JavaScript code and executes it. Returns the result of the last expression in the input string.

To be avoided where possible.

>>> eval('1+2')

3

>>> eval('parseInt("123")')

123

>>> eval('new Array(1,2,3)')

[1, 2, 3]

>>> eval('new Array(1,2,3); 1+1;')

2

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Object-Oriented JavaScript
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon