JavaScript Miscellaneous
Day 30 of 60 : Learn Full Stack Web Development with JavaScript Fundamentals.
JavaScript this keyword :
The this keyword is a reference variable that refers to the current object. Here, we will learn about this keyword with help of different examples.
JavaScript this Keyword Example :
Let's see a simple example of this keyword.
<script>
var address=
{
company:"Javatpoint",
city:"Noida",
state:"UP",
fullAddress:function()
{
return this.company+" "+this.city+" "+this.state;
}
};
var fetch=address.fullAddress();
document.writeln(fetch);
</script>
Output:
Javatpoint Noida UP
JavaScript Date Object :
The JavaScript date object can be used to get year, month and day.
You can display a timer on the webpage by the help of JavaScript date object.
You can use different Date constructors to create date object.
It provides methods to get and set day, month, year, hour, minute and seconds.
Constructor :
You can use 4 variant of Date constructor to create date object.
Date()
Date(milliseconds)
Date(dateString)
Date(year, month, day, hours, minutes, seconds, milliseconds)
JavaScript Date Methods
Let's see the list of JavaScript date methods with their description.
avaScript Date Example |
Let's see the simple example to print date object. It prints date and time both.Methods
Description | |
getDate() | It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of local time. |
getDay() | It returns the integer value between 0 and 6 that represents the day of the week on the basis of local time. |
getFullYears() | It returns the integer value that represents the year on the basis of local time. |
getHours() | It returns the integer value between 0 and 23 that represents the hours on the basis of local time. |
getMilliseconds() | It returns the integer value between 0 and 999 that represents the milliseconds on the basis of local time. |
getMinutes() | It returns the integer value between 0 and 59 that represents the minutes on the basis of local time. |
getMonth() | It returns the integer value between 0 and 11 that represents the month on the basis of local time. |
getSeconds() | It returns the integer value between 0 and 60 that represents the seconds on the basis of local time. |
getUTCDate() | It returns the integer value between 1 and 31 that represents the day for the specified date on the basis of universal time. |
getUTCDay() | It returns the integer value between 0 and 6 that represents the day of the week on the basis of universal time. |
getUTCFullYears() | It returns the integer value that represents the year on the basis of universal time. |
getUTCHours() | It returns the integer value between 0 and 23 that represents the hours on the basis of universal time. |
getUTCMinutes() | It returns the integer value between 0 and 59 that represents the minutes on the basis of universal time. |
getUTCMonth() | It returns the integer value between 0 and 11 that represents the month on the basis of universal time. |
getUTCSeconds() | It returns the integer value between 0 and 60 that represents the seconds on the basis of universal time. |
setDate() | It sets the day value for the specified date on the basis of local time. |
setDay() | It sets the particular day of the week on the basis of local time. |
setFullYears() | It sets the year value for the specified date on the basis of local time. |
setHours() | It sets the hour value for the specified date on the basis of local time. |
setMilliseconds() | It sets the millisecond value for the specified date on the basis of local time. |
setMinutes() | It sets the minute value for the specified date on the basis of local time. |
setMonth() | It sets the month value for the specified date on the basis of local time. |
setSeconds() | It sets the second value for the specified date on the basis of local time. |
setUTCDate() | It sets the day value for the specified date on the basis of universal time. |
setUTCDay() | It sets the particular day of the week on the basis of universal time. |
setUTCFullYears() | It sets the year value for the specified date on the basis of universal time. |
setUTCHours() | It sets the hour value for the specified date on the basis of universal time. |
setUTCMilliseconds() | It sets the millisecond value for the specified date on the basis of universal time. |
setUTCMinutes() | It sets the minute value for the specified date on the basis of universal time. |
setUTCMonth() | It sets the month value for the specified date on the basis of universal time. |
setUTCSeconds() | It sets the second value for the specified date on the basis of universal time. |
toDateString() | It returns the date portion of a Date object. |
toISOString() | It returns the date in the form ISO format string. |
toJSON() | It returns a string representing the Date object. It also serializes the Date object during JSON serialization. |
toString() | It returns the date in the form of string. |
toTimeString() | It returns the time portion of a Date object. |
toUTCString() | It converts the specified date in the form of string using UTC time zone. |
valueOf() | It returns the primitive value of a Date object. |
JavaScript Date Example
Let's see the simple example to print date object. It prints date and time both.
<html>
<body>
Current Date and Time: <span id="txt"></span>
<script>
var today=new Date();
document.getElementById('txt').innerHTML=today;
</script>
</body>
</html>
Output:
Current Date and Time: Sat Jun 22 2024 23:55:17 GMT+0530 (India Standard Time)
JavaScript Math :
The JavaScript math object provides several constants and methods to perform mathematical operation. Unlike date object, it doesn't have constructors.
JavaScript Math Methods
Let's see the list of JavaScript Math methods with description.
Methods | Description |
abs() | It returns the absolute value of the given number. |
acos() | It returns the arccosine of the given number in radians. |
asin() | It returns the arcsine of the given number in radians. |
atan() | It returns the arc-tangent of the given number in radians. |
cbrt() | It returns the cube root of the given number. |
ceil() | It returns a smallest integer value, greater than or equal to the given number. |
cos() | It returns the cosine of the given number. |
cosh() | It returns the hyperbolic cosine of the given number. |
exp() | It returns the exponential form of the given number. |
floor() | It returns largest integer value, lower than or equal to the given number. |
hypot() | It returns square root of sum of the squares of given numbers. |
log() | It returns natural logarithm of a number. |
max() | It returns maximum value of the given numbers. |
min() | It returns minimum value of the given numbers. |
pow() | It returns value of base to the power of exponent. |
random() | It returns random number between 0 (inclusive) and 1 (exclusive). |
round() | It returns closest integer value of the given number. |
sign() | It returns the sign of the given number |
sin() | It returns the sine of the given number. |
sinh() | It returns the hyperbolic sine of the given number. |
sqrt() | It returns the square root of the given number |
tan() | It returns the tangent of the given number. |
tanh() | It returns the hyperbolic tangent of the given number. |
trunc() | It returns an integer part of the given number. |
Example:
<!DOCTYPE html>
<html>
<body>
Square Root of 17 is: <span id="p1"></span>
<script>
document.getElementById('p1').innerHTML=Math.sqrt(17);
</script>
</body>
</html>
Output:
Square Root of 17 is: 4.123105625617661
JavaScript array.length property
The length property returns the number of elements in an array in the form of a 32-bit unsigned integer.
We can also say that the length property returns a number that represents the number of array elements.
The return value is always larger than the highest array index.
The length property can also be used to set the number of elements in an array.
We have to use the assignment operator in conjunction with the length property to set an array's length.
The array.length property in JavaScript is same as the array.size() method in jQuery.
In JavaScript, it is invalid to use array.size() method so, we use array.length property to calculate the size of an array.
Syntax :
The following syntax is used to return the length of an array
- array.length
The following syntax is used to set the length of an array
- array.length = number