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
4
Regular Expressions
5
Index

String


The String() constructor creates string objects. Primitive strings are turned into objects behind the scenes if you call a method on them as if they were objects.

Creating a string object and a string primitive:

>>> var s_obj = new String('something'); 
>>> var s_prim = 'something';
>>> typeof s_obj

"object"

>>> typeof s_prim

"string"

The object and the primitive are not equal when compared by type with ===:

>>> s_obj === s_prim

false

>>> s_obj == s_prim

true

length is a property of string objects:

>>> s_obj.length

9

If you access length on a non-object but a primitive string, the primitive is converted to an object behind the scenes and the operation is successful:

>>> "something".length

9

Members of the String Constructor

Property/Method

Description

String.fromCharCode (code1, code2, code3, ...)

Returns a string created using the input character codes:

>>> String.fromCharCode(115, 99, 114, 105, 112, 116);

"script"

Members of the String Objects

Property/Method

Description

length

The number of characters in the string.

>>> new String('four').length

4

charAt(pos)

Returns the character at the specified position. Positions start at 0.

>>> "script".charAt(0);

"s"

charCodeAt(pos)

Returns the code of the character at the specified position.

>>> "script".charCodeAt(0);

115

concat(str1, str2, ....)

Return a new string glued from the input pieces.

>>> "".concat('zig', '-', 'zag');

"zig-zag"

indexOf(needle, start)

If the needle matches a part of the string, the position of the match is returned. The optional second parameter tells where the search should start from. Returns -1 if no match is found.

>>> "javascript".indexOf('scr')

4

>>> "javascript".indexOf('scr', 5)

-1

lastIndexOf(needle, start)

Same as indexOf() but starts the search from the end of the string. The last occurence of "a":

>>> "javascript".lastIndexOf('a') 

3

localeCompare(needle)

Compares two strings in the current locale. Returns 0 if the two strings are equal, 1 if the needle gets sorted before the string object, -1 otherwise.

>>> "script".localeCompare('crypt')

1

>>> "script".localeCompare('sscript')

-1

>>> "script".localeCompare('script')

0

match(regexp)

Accepts a regular expression object and returns an array of matches.

>>> "R2-D2 and C-3PO".match(/[0-9]/g)

["2", "2", "3"]

replace(needle, replacement)

Allows you to replace the matching results of a regexp pattern. The replacement can also be a callback function. Capturing patterns are available as $1, $2,...$9.

>>> "R2-D2".replace(/2/g, '-two')

"R-two-D-two"

>>> "R2-D2".replace(/(2)/g, '$1$1')

"R22-D22"

search(regexp)

Returns the position of the first regular expression match.

>>> "C-3PO".search(/[0-9]/)

2

slice(start, end)

Returns the part of a string identified by start and end position. If start is negative, then the start position is length + start, similarly if the end parameter is negative, the end position is length + end.

>>> "R2-D2 and C-3PO".slice(4,13)

"2 and C-3"

>>> "R2-D2 and C-3PO".slice(4,-1)

"2 and C-3P"

split(separator, limit)

Turns a string into an array. The second parameter, limit, is optional. The separator can also be a regular expression.

>>> "1,2,3,4".split(',')

["1", "2", "3", "4"]

>>> "1,2,3,4".split(',', 2)

["1", "2"]

substring(start, end)

Similar to slice(). When start or end are negative or invalid, they are considered 0. If they are greater than the string length, they are considered to be the length. If end > start, their values are swapped.

>>> "R2-D2 and C-3PO".substring(4, 13)

"2 and C-3"

>>> "R2-D2 and C-3PO".substring(13, 4)

"2 and C-3"

toLowerCase()

toLocaleLowerCase()

Transforms the string to lower case.

>>> "JAVA".toLowerCase()

"java"

toUpperCase()

toLocaleUpperCase()

Transforms the string to upper case.

>>> "script".toUpperCase()

"SCRIPT"

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