You are on page 1of 30

STRING FUNCTIONS

JAVSCRIPT
CHARAT()
• Returns the character at specified index
• var str = "HELLO WORLD";
var n = str.charAt(2)
• Output = L
CONCAT()
• Joins two or more strings, and returns a copy of the
joined strings
• var str1 = "Hello ";
var str2 = "world!";
var n = str1.concat(str2);
• Output = Hello World!
INDEXOF()
• Returns the position of the first found occurrence of
a specified value in a string.
• var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
• Output = 13
LASTINDEXOF()
• Returns the position of the last found occurrence of
a specified value in a string.
• var str="Hello planet earth, you are a great planet.";
var n=str.lastIndexOf("planet");
• Output = 36
MATCH()
• Searches for a match between a regular expression
and a string, and returns the matches.
• var str="The rain in SPAIN stays mainly in the plain"; 
var n=str.match(/ain/g);
• Output = ain,ain,ain
REPLACE()
• Searches for a match between a substring (or
regular expression) and a string, and replaces the
matched substring with a new substring
• var str="Visit Microsoft!";
var n=str.replace("Microsoft",“Sun Systems");
• Output = Visit Sun Systems
SEARCH()
• Searches for a match between a regular expression
and a string, and returns the position of the match.
• var str="Visit W3Schools!";
var n=str.search("W3Schools");
• Output = 6
SLICE()
• Extracts a part of a string and returns a new string
• var str="Hello world!";
var n=str.slice(1,5);
• Output = ello
SPLIT()
• Splits a string into an array of substrings.
• var str="How are you doing today?";
var n=str.split(" ");
• Output = How, are, you, doing, today?
SUBSTR()
• Extracts the characters from a string, beginning at a
specified start position, and through the specified
number of character.
• var str="Hello world!";
var n=str.substr(2,3)
• Output = llo
SUBSTRING()
• Extracts the characters from a string, between two
specified indices.
• <script>
var str="Hello world!";
document.write(str.substring(3)+"<br>");
document.write(str.substring(3,7));
</script>
• Output = lo world!
lo w
TOLOWERCASE()
• Converts a string to lowercase letters.
• <script>
var str="Hello World!";
document.write(str.toLowerCase());
</script>
• Output = hello world!
TOUPPERCASE()
• Converts a string to uppercase letters.
• <script>
var str="Hello world!";
document.write(str.toUpperCase());
</script>
• Output = HELLO WORLD!
VALUEOF()
• Returns the primitive value of a String object.
• <script>
var str="Hello world!";
document.write(str.valueOf());
</script>
• Output = Hello World!
MATHS FUNCTIONS

JAVASCRIPT
MAX()
• Returns the number with the highest value of x and y.
• <html>
<body>
<script type="text/javascript">
document.write(Math.max(2,4))
</script>
</body>
</html>
MIN()
• Returns the number with the lowest value of x and y.
• <html>
<body>
<script type="text/javascript">
document.write(Math.min(2,4))
</script>
</body>
</html>
ROUND()
• Rounds x to the nearest integer.
• <html>
<body>
<script type="text/javascript">
document.write(Math.round(7.25))
</script>
</body>
</html>
RANDOM()
• Returns a random number between 0 and 1.
• <html>
<body>
<script type="text/javascript">
document.write(Math.random())
</script>
</body>
</html>
DATE FUNCTIONS

JAVASCRIPT
DATE()
• Returns today's date including date, month, and year.
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getDate())
document.write(".")
document.write(d.getMonth() + 1)
document.write(".")
document.write(d.getFullYear())
</script>
</body>
</html>
GETDATE()
• Returns the date of a Date object (from 1-31)
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getDate())
</script>
</body>
</html>
GETDAY()
• Returns the day of a Date object (from 0-6. 0=Sunday,
1=Monday, etc.)
• <html>
<body>
<script type="text/javascript">
var d = new Date()
var weekday=new
Array("Sun","Mon","Tues","Wed","Thur","Fri","Sat")
document.write("Today is " + weekday[d.getDay()])
</script>
</body>
</html>
GETMONTH()
• Returns the month of a Date object (from 0-11.
0=January, 1=February, etc.).
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getMonth() + 1)
</script>
</body>
</html>
GETFULLYEAR()
• Returns the year of the Date object (four digits).
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getFullYear())
</script>
</body>
</html>
GETHOURS()
• Returns the hour of the Date object (from 0-23).
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getHours())
</script>
</body>
</html>
GETMINUTES()
• Returns the minute of the Date object (from 0-59).
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getMinutes() )
</script>
</body>
</html>
GETSECONDS()
• Returns the second of the Date object (from 0-59).
• <html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getSeconds())
</script>
</body>
</html>
SETDATE, SETMONTH, SETFULLYEAR
• You can also set the date or time into the date
object, with the setDate, setHour etc.
• <html>
<body>
<script type="text/javascript">
var d = new Date()
d.setFullYear("1990")
document.write(".")
</script>
</body>
</html>

You might also like