Built-In Objects in Javascript
Built-In Objects in Javascript
var num=100;
document.write("<h3> " +Math.sqrt(num)+"</h3>");
document.write("<h3> " +Math.ceil(num)+"</h3>");
document.write("<h3> " +Math.floor(num)+"</h3>");
</script>
</body>
</html>
2. Number Object
Output:
1.8907e+1
19
18.9
string
18.907
3.Date Object
• Date is a data type.
• Date object manipulates date and time.
• Date() constructor takes no arguments.
• Date object allows you to get and set the year, month,
day, hour, minute, second and millisecond fields.
Syntax:
var variable_name = new Date();
Example:
var current_date = new Date();
Date Object Methods
Methods Description
Date() Returns current date and time.
getDate() Returns the day of the month.
getDay() Returns the day of the week.
getFullYear() Returns the year.
getHours() Returns the hour.
getMinutes() Returns the minutes.
getSeconds() Returns the seconds.
getMilliseconds() Returns the milliseconds.
Output:
Date Methods
Locale String: 24/4/2023, 8:57:07 pm
Hours: 20
FullYear: 2023
Minutes: 57
4.Boolean Object
• The Boolean object represents two values,
either "true" or "false".
• If value parameter is omitted or is 0, -0, null,
false, NaN, undefined, or the empty string (""),
the object has an initial value of false.
Syntax
• Use the following syntax to create a boolean
object.
var val = new Boolean(value);
Boolean Object Methods
<html>
<head>
<title>JavaScript valueOf() Method</title>
</head>
<body>
<script type = "text/javascript">
var flag = new Boolean(false);
document.write( "flag.valueOf is : " + flag.valueOf() );
</script>
</body>
</html>
Output
flag.valueOf is : false
5.String Object
• String objects are used to work with text.
• It works with a series of characters.
• Syntax:
var variable_name = new String(string);
Example:
var s = new String(string);
String Object
Methods
Methods Description
charAt() It returns the character at the specified index.
It returns the ASCII code of the character at the specified
charCodeAt() position.
concat() It combines the text of two strings and returns a new string.
indexOf() It returns the index within the calling String object.
match() It is used to match a regular expression against a string.
replace() It is used to replace the matched substring with a new substring.
search() It executes the search for a match between a regular expression.
slice() It extracts a session of a string and returns a new string.
Window Object
• The window object represents a window in
browser. An object of window is created
automatically by the browser.
• Window is the object of browser, it is not the
object of javascript. The javascript objects are
string, array, date etc.
Window Object Methods
Method Description
displays the alert box containing message with
alert()
ok button.
displays the confirm dialog box containing
confirm()
message with ok and cancel button.
prompt() displays a dialog box to get input from the user.
open() opens the new window.
close() closes the current window.
performs action after specified time like calling
setTimeout()
function, evaluating expressions etc.
<html>
<head >
<title> window object </title>
</head >
<body>
<script type="text/javascript">
function msg(){
alert("Hello Alert Box");
}
</script>
<input type="button" value="click" onclick="msg()"/>
</body>
</html>
• https://www.javatpoint.com/window-object
Regular Expression
RegExp Object
• A regular expression is a pattern of characters.
• The pattern is used to do pattern-matching
"search-and-replace" functions on text.
• In JavaScript, a RegExp Object is a pattern
with Properties and Methods.
Syntax
/pattern/modifier(s);
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p>Do a case-insensitive search for "w3schools" in a string:</p>
<p id="demo"></p>
<script>
let text = "Visit W3Schools";
let pattern = /w3schools/i;
let result = text.match(pattern);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
Example explained:
w3schools -The pattern to search for
/w3schools/ -A regular expression
/w3schools/i -A case-insensitive regular
expression
JavaScript Errors
(Exceptions)
Throw, and Try...Catch...Finally
• The try statement defines a code block to run
(to try).
• The catch statement defines a code block to
handle any error.
• The finally statement defines a code block to
run regardless of the result.
• The throw statement defines a custom error.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Error Handling</h2>
<p>How to use <b>catch</b> to display an error.</p>
<p id="demo"></p>
<script>
try {
addlert("Welcome guest!");
}
catch(err) {
document.getElementById("demo").innerHTML = err.message;
}
</script>
</body>
</html>
JavaScript try and catch
• The try statement allows you to define a block of code to be
tested for errors while it is being executed.
• The catch statement allows you to define a block of code to be
executed, if an error occurs in the try block.
• The JavaScript statements try and catch come in pairs:
try {
Block of code to try
}
catch(err) {
Block of code to handle errors
}
The throw Statement
• The throw statement allows you to create a
custom error.
• Technically you can throw an exception (throw
an error).
• The exception can be a JavaScript String, a
Number, a Boolean or an Object:
throw "Too big"; //throw a text
throw 500; // throw a number
JavaScript Events
• The change in the state of an object is known as an
Event.
• In html, there are various events which represents that
some activity is performed by the user or by the
browser.
• When javascript code is included in HTML, js react over
these events and allow the execution. This process of
reacting over the events is called Event Handling. Thus,
js handles the HTML events via Event Handlers.
• For example, when a user clicks over the browser, add
js code, which will execute the task to be performed on
the event.
Mouse events
Answer: onclick
Exercise-2
<button----------- =“---------------">Click
me.</button>
Answer: onclick
myFunction()
Exercise-3
<div ------------="this.style.backgroundColor='red'">myDIV.</div>
Answer:
onmouseover
Validation
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Validation</h2>
<p>If you click submit, without filling out the text field,
your browser will display an error message.</p>
</body>
</html>