JavaScript ObjectExamples
Examples using built-in JavaScript objects.
String Object
- Return the length of a string
- Add style 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, and if found, return it - match() method
- Replace characters in a string - replace()
- For more string object examples, see 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 today's date (according to UTC) to a string
- Use getDay() to display the weekday, not just a number
- Display a clock
- For more Date object examples, see ourJavaScript Date Object reference manual.
Array Object
- Create an array
- Join two arrays - concat()
- Join three arrays - concat()
- Form a string from array elements - join()
- Remove the last element of an array - pop()
- Add new elements 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()
- Array sorting (ascending alphabetical order) - sort()
- Numeric sorting (ascending numerical order) - sort()
- Numeric sorting (descending numerical order) - sort()
- Add an element at position 2 in an array - splice()
- Convert an array to a string - toString()
- Add new elements to the beginning of an array - unshift()
- For more Array object examples, see ourJavaScript Array Object reference manual.
Boolean Object
- Check logical values
- For more Boolean object examples, see 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
General
- Use the for...in statement on object elements
- For more Math object examples, see ourJavaScript Math Object reference manual.