CONTENTS
String methods in JavaScript
* explain different string methods
Number methods in JavaScript
* explain different number methods
Math objects in JavaScript
* explain Math objects in Java Script
JavaScript Dates
* explain JavaScript Date Objects
* explain JavaScript Date Formats
Extracting String Characters
There are 3 methods for extracting string
characters:
charAt(position)
charCodeAt(position)
Property access [ ]
JavaScript String charAt()
The charAt() method returns the character at a
specified index (position) in a string:
Example
let text = "HELLO WORLD";
let char = [Link](0);
Output: H
JavaScript String charCodeAt()
The charCodeAt() method returns the unicode of the
character at a specified index in a string:
Example
let text = "HELLO WORLD";
let char = [Link](0);
Output: 72
Property Access
let text = "HELLO WORLD";
let char = text[0];
output: H
JavaScript String split()
A string can be converted to an array with the
split() method:
[Link](",") // Split on commas
[Link](" ") // Split on spaces
[Link]("|") // Split on pipe
JavaScript Search Methods
1. String indexOf()
2. String lastIndexOf()
3. String startsWith()
4. String endsWith()
JavaScript String indexOf()
The indexOf() method returns the index of (the position
of) the first occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
[Link]("locate");
output: 7
JavaScript String lastIndexOf()
The lastIndexOf() method returns the index of
the last occurrence of a specified text in a string:
Example
let str = "Please locate where 'locate' occurs!";
[Link]("locate");
output: 21
Both indexOf(), and lastIndexOf() return -1 if
the text is not found:
Example
let str = "Please locate where 'locate' occurs!";
[Link]("John");
output: -1
JavaScript String startsWith()
The startsWith() method returns true if a string
begins with a specified value, otherwise false:
Example
let text = "Hello world, welcome to the universe.";
[Link]("Hello");
output: true
let text = "Hello world, welcome to the universe.";
[Link]("world", 5) // Returns false
let text = "Hello world, welcome to the universe.";
[Link]("world", 6) // Returns true
JavaScript Number
Methods
toExponential() Method
toExponential() returns a string, with a number
rounded and written using exponential notation.
A parameter defines the number of characters behind the
decimal point:
Example
let x = 9.656;
[Link](2); // 9.66e+0
[Link](4); // 9.6560e+0
[Link](6); // 9.656000e+0
The toFixed() Method
toFixed() returns a string, with the number written
with a specified number of decimals:
Example
let x = 9.656;
[Link](0); // 10
[Link](2); // 9.66
[Link](4); // 9.6560
[Link](6); // 9.656000
toPrecision() Method
toPrecision() returns a string, with a number written
with a specified length:
Example
let x = 9.656;
[Link](); // 9.656
[Link](2); // 9.7
[Link](4); // 9.656
[Link](6); // 9.65600
Converting Variables to Numbers
There are 3 JavaScript methods that can be used
to convert variables to numbers:
1 The Number() method
2 The parseInt() method
3 The parseFloat() method
These methods are not number methods, but
global JavaScript methods.
TheNumber() Method
Number() can be used to convert JavaScript
variables to numbers.
Example
Number(true); // returns 1
Number(false); // returns 0
Number("25"); // returns 25
Number("25,77"); // returns NaN
If the number cannot be converted, NaN(Not a Number) is
returned.
The parseFloat() Method
parseFloat( ) parses a string and returns a number.
Spaces are allowed. Only the first number is returned:
Example
parseFloat("25"); // returns 25
parseFloat("25.77"); // returns 25.77
parseFloat("255075"); // returns 25
parseFloat("25 years"); // returns 25
parseFloat("years 25"); // returns NaN
Math objects in JavaScript
The JavaScript Math object allows you to perform
mathematical tasks on numbers.
Example
[Link];
[Link] returns the ratio of a circle's circumference to
its diameter:
3.141592653589793
Math Properties (Constants)
The syntax for any Math property is :[Link]
Example
Math.E // returns Euler's number
[Link] // returns PI
Math.SQRT2 // returns the square root of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math Methods
The syntax for Math any methods is : [Link](number)
Number to Integer
There are 4 common methods to round a number to an integer:
[Link](x) Returns x rounded to its nearest integer
[Link](x) Returns x rounded up to its nearest integer
[Link](x) Returns x rounded down to its nearest integer
[Link](x) Returns the integer part of x (new in ES6)
[Link]()
[Link](x) returns the nearest integer:
Examples
[Link](4.6); // 5
[Link](4.5); // 5
[Link](4.4); // 4
[Link]()
[Link](x) returns the value of x rounded up
to its nearest integer:
Example
[Link](4.9); // 5
[Link](4.7); // 5
[Link](4.4); // 5
[Link](4.2); // 5
[Link](-4.2); // -4
[Link]()
[Link](x) returns the value of x rounded
down to its nearest integer:
Example
[Link](4.9); // 4
[Link](4.7); // 4
[Link](4.4); // 4
[Link](4.2); // 4
[Link](-4.2); // -5
[Link]()
[Link](x) returns the integer part of x:
Example
[Link](4.9); // 4
[Link](4.7); // 4
[Link](4.4); // 4
[Link](4.2); // 4
[Link](-4.2); // -4
[Link]()
[Link](x) returns if x is negative, null or
positive:
Example
[Link](-4); // -4
[Link](0); // 0
[Link](4); // 4
[Link]()
[Link](x, y) returns the value of x to the
power of y:
Example
[Link](8, 2); // 64
[Link]()
[Link](x) returns the square root of x:
Example
[Link](64); // 8
[Link]()
[Link](x) returns the absolute (positive)
value of x:
Example
[Link](-4.7); // 4.7
[Link]()
[Link](x) returns the sine (a value between -1 and 1)
of the angle x (given in radians).
If you want to use degrees instead of radians, you have
to convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
[Link](90 * [Link] / 180); // returns 1
(the sine of 90 degrees)
[Link]()
[Link](x) returns the cosine (a value between -1 and 1)
of the angle x (given in radians).
If you want to use degrees instead of radians, you have to
convert degrees to radians:
Angle in radians = Angle in degrees x PI / 180.
Example
[Link](0 * [Link] / 180); // returns 1
(the cos of 0 degrees)
[Link]() and [Link]()
[Link]() and [Link]() can be used to find the
lowest or highest value in a list of arguments:
Example
[Link](0, 150, 30, 20, -8, -200); // -200
[Link](0, 150, 30, 20, -8, -200); // 150
[Link]()
[Link]() returns a random number between
0 (inclusive), and 1 (exclusive):
Example
[Link]();
0.2941035364708071
0.9759494259009838
0.8881937367957491
0.7322554226597067
The [Link]() Method
[Link](x) returns the natural logarithm of x.
Examples
[Link](1); // 0
[Link](2); // 0.6931471805599453
The Math.log2() Method
Math.log2(x) returns the base 2 logarithm of x.
Examples
Math.log2(8); // 3
The Math.log10() Method
Math.log10(x) returns the base 10 logarithm of x.
Examples
Math.log10(1000); // 3
JavaScript Math Methods
Method Description
abs(x) Returns the absolute value of x
acos(x) Returns the arccosine of x, in radians
asin(x) Returns the arcsine of x, in radians
atan(x) Returns the arctangent of x as a numeric
value between -PI/2 and PI/2 radians
atan2(y, x) Returns the arctangent of the quotient
of its arguments
ceil(x) Returns the value of x rounded up to
its nearest integer
cos(x) Returns the cosine of x (x is in
radians)
exp(x) Returns the value of Ex
floor(x) Returns the value of x rounded down
to its nearest integer
log(x) Returns the natural logarithm (base E)
of x
max(x, y, z, ..., n) Returns the number with the highest
value
min(x, y, z, ..., n) Returns the number with the lowest
value
pow(x, y) Returns the value of x to the power of
y
random() Returns a random number between 0
and 1
round(x) Returns the value of x rounded
to its nearest integer
sin(x) Returns the sine of x (x is in
radians)
sqrt(x) Returns the square root of x
tan(x) Returns the tangent of an angle
JavaScript Dates
explain JavaScript Date Objects
explain JavaScript Date Formats
explain JavaScript Date get methods
explain JavaScript Date set methods
JavaScript Date Objects
By default, JavaScript will use the browser's
time zone and display a date as a full text string.
Thu Sep 27 2018 09:09:39 GMT+0530 (India
Standard Time
Creating Date Objects
Date objects are created with the new Date()
constructor.
There are 4 ways to create a new date object. They are
1 new Date()
2 new Date(year, month, day, hours,
minutes, seconds, milliseconds)
3 new Date(date string)
4 new Date(milliseconds)
new Date()
new Date() creates a new date object with the
current date and time:
Example
const d = new Date();
Fri Oct 07 2022 14:22:54 GMT+0530 (India
Standard Time)
new Date(year, month, day, hours, minutes, seconds,
milliseconds)
new Date(year, month, ...) creates a new date object with
a specified date and time.
Example
const d = new Date(2018, 11, 24, 10, 33, 30, 0);
Mon Dec 24 2018 10:33:30 GMT+0530 (India Standard Time)
new Date(dateString)
new Date(dateString) creates a new date object
from a date string:
Example
const d = new Date("October 13, 2014 11:13:00");
Mon Oct 13 2014 11:13:00 GMT+0530 (India
Standard Time)
new Date(milliseconds)
new Date(milliseconds) creates a new date object
as zero time plus milliseconds:
Example
const d = new Date(0);
01 January 1970 plus 100 000 000 milliseconds is
approximately 02 January 1970.
Date Methods
Date methods allow you to get and set the
year, month, day, hour, minute, second, and
millisecond of date objects, using either local time
or UTC (universal, or GMT) time.
Displaying Dates
By default JavaScript will output dates in full text
string format.
When you display a date object in HTML, it is
automatically converted to a string, with the toString()
method.
Example
const d = new Date();
alert([Link]());
Fri Oct 07 2022 14:43:03 GMT+0530 (India
Standard Time)
Example
var d = new Date();
alert(d);
Example
var d = new Date();
alert([Link]());
JavaScript Date Formats
JavaScript Date Input
There are generally 3 types of JavaScript date input formats .