JavaScript Object Examples
Examples using built-in JavaScript objects.
String object
- Return the length of a string
- Add styles to a string
- Return the position of the first occurrence of specified text in a string - indexOf() method
- Find a specific character in a string; if found, return it - match() method
- Replace characters in a string - replace()
- More string object examples, in ourJavaScript String Object Reference Manual.
Date object
- Use the Date() method to return today's date and time
- Use getTime() to calculate how many milliseconds from 1970 to today
- Use setFullYear() to set a specific date
- Use toUTCString() to convert the current date (according to UTC) to a string
- Use getDay() to display the day of the week, not just a number
- Display a clock
- More Date object examples, in ourJavaScript Date Object Reference Manual.
Array object
- Creating an Array
- Combine two arrays - concat()
- Combine three arrays - concat()
- Make a string from the elements of an array - join()
- Remove the last element of an array - pop()
- Add a new element to the end of an array - push()
- Reverse the order of elements in an array - reverse()
- Remove the first element of an array - shift()
- Select elements from an array - slice()
- Sort an array (alphabetically ascending) - sort()
- Sort numbers (numerically ascending) - sort()
- Sort numbers (numerically descending) - sort()
- Add an element at position 2 of an array - splice()
- Convert an array to a string - toString()
- Add a new element at the beginning of an array - unshift()
- More Array object examples, in ourJavaScript Array Object Reference Manual.
Boolean object
- Check a logical value
- More Boolean object examples, in ourJavaScript Boolean Object Reference Manual.
Math object
- Use round() to round a number
- Use random() to return a random number between 0 and 1
- Use max() to return the larger of two given numbers
- Use min() to return the smaller of two given numbers
- Celsius to Fahrenheit conversion
Average
- Use the for...in statement on object elements
- More Math object examples in ourJavaScript Math Object Reference Manual.