Javascript String Object Tutorial

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

String Object

The String object is used to manipulate a stored piece of text. String objects are created with new String().

Syntax
var txt = new String("string"); or more simply: var txt = "string"; For a tutorial about the String object, read our JavaScript String Object tutorial.

String Object Properties


Property constructor length prototype Description Returns the function that created the String object's prototype Returns the length of a string Allows you to add properties and methods to an object

String Object Methods


Method charAt() charCodeAt() concat() fromCharCode() indexOf() lastIndexOf() match() replace() search() Description Returns the character at the specified index Returns the Unicode of the character at the specified index Joins two or more strings, and returns a copy of the joined strings Converts Unicode values to characters Returns the position of the first found occurrence of a specified value in a string Returns the position of the last found occurrence of a specified value in a string Searches for a match between a regular expression and a string, and returns the matches Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring Searches for a match between a regular expression and a string, and returns

slice() split() substr() substring() toLowerCase() toUpperCase() valueOf()

the position of the match Extracts a part of a string and returns a new string Splits a string into an array of substrings Extracts the characters from a string, beginning at a specified start position, and through the specified number of character Extracts the characters from a string, between two specified indices Converts a string to lowercase letters Converts a string to uppercase letters Returns the primitive value of a String object

String HTML Wrapper Methods


The HTML wrapper methods return the string wrapped inside the appropriate HTML tag. Method Description anchor() Creates an anchor big() Displays a string using a big font blink() Displays a blinking string bold() Displays a string in bold fixed() Displays a string using a fixed-pitch font fontcolor() Displays a string using a specified color fontsize() Displays a string using a specified size italics() Displays a string in italic link() Displays a string as a hyperlink small() Displays a string using a small font strike() Displays a string with a strikethrough sub() Displays a string as subscript text sup() Displays a string as superscript text

Date Object
The Date object is used to work with dates and times.

Date objects are created with new Date(). There are four ways of instantiating a date:
var d = new Date(); var d = new Date(milliseconds); var d = new Date(dateString); var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

For a tutorial about date and times, read our JavaScript Date Object tutorial.

Date Object Properties


Property constructor prototype Description Returns the function that created the Date object's prototype Allows you to add properties and methods to an object

Date Object Methods


Method getDate() getDay() getFullYear() getHours() getMilliseconds() getMinutes() getMonth() getSeconds() getTime() Description Returns the day of the month (from 1-31) Returns the day of the week (from 0-6) Returns the year (four digits) Returns the hour (from 0-23) Returns the milliseconds (from 0-999) Returns the minutes (from 0-59) Returns the month (from 0-11) Returns the seconds (from 0-59) Returns the number of milliseconds since midnight Jan 1, 1970

getTimezoneOffset() getUTCDate() getUTCDay() getUTCFullYear() getUTCHours() getUTCMilliseconds() getUTCMinutes() getUTCMonth() getUTCSeconds() getYear() parse() setDate() setFullYear() setHours() setMilliseconds() setMinutes() setMonth() setSeconds() setTime() setUTCDate() setUTCFullYear()

Returns the time difference between UTC time and local time, in minutes Returns the day of the month, according to universal time (from 1-31) Returns the day of the week, according to universal time (from 0-6) Returns the year, according to universal time (four digits) Returns the hour, according to universal time (from 0-23) Returns the milliseconds, according to universal time (from 0-999) Returns the minutes, according to universal time (from 0-59) Returns the month, according to universal time (from 0-11) Returns the seconds, according to universal time (from 0-59) Deprecated. Use the getFullYear() method instead Parses a date string and returns the number of milliseconds since midnight of January 1, 1970 Sets the day of the month of a date object Sets the year (four digits) of a date object Sets the hour of a date object Sets the milliseconds of a date object Set the minutes of a date object Sets the month of a date object Sets the seconds of a date object Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 Sets the day of the month of a date object, according to universal time Sets the year of a date object, according to universal time (four digits)

setUTCHours() setUTCMilliseconds() setUTCMinutes() setUTCMonth() setUTCSeconds() setYear() toDateString() toGMTString() toISOString() toJSON() toLocaleDateString() toLocaleTimeString() toLocaleString() toString() toTimeString() toUTCString() UTC() valueOf()

Sets the hour of a date object, according to universal time Sets the milliseconds of a date object, according to universal time Set the minutes of a date object, according to universal time Sets the month of a date object, according to universal time Set the seconds of a date object, according to universal time Deprecated. Use the setFullYear() method instead Converts the date portion of a Date object into a readable string Deprecated. Use the toUTCString() method instead Returns the date as a string, using the ISO standard Returns the date as a string, formated as a JSON date Returns the date portion of a Date object as a string, using locale conventions Returns the time portion of a Date object as a string, using locale conventions Converts a Date object to a string, using locale conventions Converts a Date object to a string Converts the time portion of a Date object to a string Converts a Date object to a string, according to universal time Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time Returns the primitive value of a Date object

Array Object
The Array object is used to store multiple values in a single variable.

For a tutorial about Arrays, read our JavaScript Array Object tutorial.

Array Object Properties


Property constructor length prototype Description Returns the function that created the Array object's prototype Sets or returns the number of elements in an array Allows you to add properties and methods to an Array object

Array Object Methods


Method concat() indexOf() join() lastIndexOf() pop() push() reverse() shift() slice() sort() splice() toString() Description Joins two or more arrays, and returns a copy of the joined arrays Search the array for an element and returns it's position Joins all elements of an array into a string Search the array for an element, starting at the end, and returns it's position Removes the last element of an array, and returns that element Adds new elements to the end of an array, and returns the new length Reverses the order of the elements in an array Removes the first element of an array, and returns that element Selects a part of an array, and returns the new array Sorts the elements of an array Adds/Removes elements from an array Converts an array to a string, and returns the result

unshift() valueOf()

Adds new elements to the beginning of an array, and returns the new length Returns the primitive value of an array

Boolean Object
The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false). For a tutorial about the Boolean object, read our JavaScript Boolean Object tutorial.

Boolean Object Properties


Property constructor prototype Description Returns the function that created the Boolean object's prototype Allows you to add properties and methods to a Boolean object

Boolean Object Methods


Method toString() valueOf() Description Converts a Boolean value to a string, and returns the result Returns the primitive value of a Boolean object

Math Object
The Math object allows you to perform mathematical tasks. Math is not a constructor. All properties/methods of

Math can be called by using Math as an object, without creating it.

Syntax
var x = Math.PI; // Returns PI var y = Math.sqrt(16); // Returns the square root of 16

For a tutorial about the Math object, read our JavaScript Math Object tutorial.

Math Object Properties


Property Description Returns Euler's number (approx. 2.718) Returns the natural logarithm of 2 (approx. 0.693) Returns the natural logarithm of 10 (approx. 2.302) Returns the base-2 logarithm of E (approx.

LN2

LN10

LOG2E

1.442) Returns the base-10 LOG10E logarithm of E (approx. 0.434) PI Returns PI (approx. 3.14)

Returns the square root of SQRT1_2 1/2 (approx. 0.707) Returns the square root of 2 (approx. 1.414)

SQRT2

Math Object Methods


Method Description Returns the absolute value of x Returns the arccosine of x, in radians Returns the arcsine of x, in radians Returns the arctangent of x as a

abs(x)

acos(x)

asin(x)

atan(x)

numeric value between PI/2 and PI/2 radians Returns the arctangent of the quotient of its arguments Returns x, rounded upwards to the nearest integer Returns the cosine of x (x is in radians) Returns the value of Ex Returns x, rounded downwards to the nearest integer Returns the natural logarithm (base E) of x

atan2(y,x)

ceil(x)

cos(x)

exp(x)

floor(x)

log(x)

Returns the number max(x,y,z,...,n) with the highest value Returns the number min(x,y,z,...,n) with the lowest value Returns the value of x to the power of y Returns a random number between 0 and 1 Rounds x to the nearest integer Returns the sine of x (x is in radians) Returns the square root of x Returns the tangent of an angle

pow(x,y)

random()

round(x)

sin(x)

sqrt(x)

tan(x)

You might also like