Unit 3
Unit 3
Applications of JavaScript:
● Web Development: Adding interactivity and behavior to static sites JavaScript
was invented to do this in 1995. By using AngularJS that can be achieved so
easily.
● Web Applications:When we explore a map in Google Maps then we only need to
click and drag the mouse. All detailed views are just a click away, and this is
possible only because of JavaScript. It uses Application Programming
Interfaces(APIs) that provide extra power to the code.
● Server Applications: With the help of Node.js, JavaScript made its way from
client to server and node.js is the most powerful in the server-side.
● Games: Not only in websites, but JavaScript also helps in creating games for
leisure. The combination of JavaScript and HTML 5 makes JavaScript popular in
game development as well.
● Smartwatches: JavaScript is being used in all possible devices and applications.
It provides a library called PebbleJS which is used in smartwatch applications.
This framework works for applications that require the internet for its
functioning.
● Art: Artists and designers can create whatever they want using JavaScript to
draw on HTML 5 canvas, make the sound more effective also can be
used p5.js library.
● Machine Learning: This JavaScript ml5.js library can be used in web
development by using machine learning.
Limitations of JavaScript:
Performance: JavaScript does not provide the same level of performance as offered
by many traditional languages as a complex program written in JavaScript would be
comparatively slow. But as JavaScript is used to perform simple tasks in a browser,
performance is not considered a big restriction in its use.
Complexity: To master a scripting language, programmers must have a thorough
knowledge of all the programming concepts, core language objects, client and
server-side objects otherwise it would be difficult for them to write advanced scripts
using JavaScript.
Weak error handling and type checking facilities: It is weakly typed language as
there is no need to specify the data type of the variable. So wrong type checking is
not performed by compile.
Advantages of JavaScript
● Speed. Client-side JavaScript is very fast because it can be run immediately within
the client-side browser. Unless outside resources are required, JavaScript is
unhindered by network calls to a backend server.
● Simplicity. JavaScript is relatively simple to learn and implement.
● Popularity. JavaScript is used everywhere on the web.
● Interoperability. JavaScript plays nicely with other languages and can be used in a
huge variety of applications.
● Server Load. Being client-side reduces the demand on the website server.
● Gives the ability to create rich interfaces.
Disadvantages of JavaScript
● Client-Side Security. Because the code executes on the users’ computer, in some
cases it can be exploited for malicious purposes. This is one reason some people
choose to disable Javascript.
● Browser Support. JavaScript is sometimes interpreted differently by different
browsers. This makes it somewhat difficult to write cross-browser code.
Q2: Define Data Type. Explain various Data Types available in Javascript.
Ans: A data type in programming refers to a classification that specifies which
type of value a variable can hold, what operations can be performed on it, and how it
is stored in memory.
Data types are fundamental in programming languages because they help ensure
data integrity, enable efficient memory allocation, and provide clarity about how data
should be used and manipulated.
JavaScript provides different data types to hold different types of values.
Object: Represents a collection of key-value pairs, where keys are strings (or
symbols) and values can be of any data type. Objects are used for more
complex data structures and can hold functions and other objects.
I. If Statement
The if statement is a fundamental control flow statement in JavaScript, used for
conditional execution of code blocks based on specified conditions. Use
the if statement to specify a block of JavaScript code to be executed if a condition is
true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Example: Make a "Good day" greeting if the hour is less than 18:00:
if (hour < 18) {
greeting = "Good day";
}
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example: If the hour is less than 18, create a "Good day" greeting, otherwise "Good
evening":
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Good day
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example: If time is less than 10:00, create a "Good morning" greeting, if not, but time is
less than 20:00, create a "Good day" greeting, otherwise a "Good evening":
if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
Good day
Example
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
Sunday
JavaScript Loops
Loops are handy to run the same code over and over again, each time with a
different value.Loops enable you to repeat a set of instructions multiple times
without having to write them out explicitly for each iteration. This saves time and
reduces redundancy in your code.
● While Loop
The while loop executes a block of code as long as the specified condition evaluates to
true. It checks the condition before executing the loop body. The while statement will
execute a block of code only when a condition is true.
while (condition)
{
code to be executed
}
Example:
<html>
<body>
<script type="text/javascript">
i=0
while (i<=5)
{
document.write("<b>The number is " + i + "</b>")
document.write("<br>")
i++
}
</script>
<p>Explanation:
<p>The for loop sets <b>i</b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 5, the loop will continue to run.
<p><b>i</b> will increase by 1 each time the loop runs.
</body>
</html>
● Do...while
Similar to the while loop, but it checks the condition after executing the block of code.
This means the block of code will always execute at least once.The do...while statement
will execute a block of code once, and then it will repeat the loop while a condition is
true.
do
{
code to be executed
}
while (condition)
Example:
<html>
<body>
<script type="text/javascript">
i=0
do
{
document.write("<b>The number is " + i + "</b>")
document.write("<br>")
i++
}
while (i<=5)
</script>
<p>Explanation:
<p>The for loop sets <b>i</b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 5, the loop will continue to run.
<p><b>i</b> will increase by 1 each time the loop runs.
</body>
</html>
● For Loop
The for loop is one of the most common looping structures. It consists of three parts:
initialization, condition, and iteration. The for statement will execute a block of code a
specified number of times.
for (initialization; condition; increment)
{
code to be executed
}
Example:
<html>
<body>
<script type="text/javascript">
for (i=0; i<=5; i++)
{
document.write("<b>The number is " + i + "</b>")
document.write("<br>")
}
</script>
<p>Explanation:
<p>The for loop sets <b>i</b> equal to 0.
<p>As long as <b>i</b> is less than or equal to 5, the loop will continue to run.
<p><b>i</b> will increase by 1 each time the loop runs.
</body>
</html>
Syntax
var sub = new String(string);
String Properties
Here is a list of the properties of String objects and their description.
constructor
Returns a reference to the String function that created the object.
length
Returns the length of the string.
prototype
The prototype property allows us to add properties and methods to an
object.
String Methods
Here is a list of the methods available in String objects along with their description.
charAt()
Returns the character at the specified index.
concat()
Combines the text of two strings and returns a new string.
indexOf()
Returns the index within the calling String object of the first occurrence
of the specified value, or -1 if not found.
lastIndexOf()
Returns the index within the calling String object of the last occurrence
of the specified value, or -1 if not found.
match()
Used to match a regular expression against a string.
replace()
Used to find a match between a regular expression and a string, and to
replace the matched substring with a new substring.
search()
Executes the search for a match between a regular expression and a
specified string.
split()
Splits a String object into an array of strings by separating the string
into substrings.
substr()
Returns the characters in a string beginning at the specified location
through the specified number of characters.
toLowerCase()
Returns the calling string value converted to lower case.
toUpperCase()
Returns the calling string value converted to uppercase.
Date Properties
Here is a list of the properties of the Date object along with their description.
constructor
Specifies the function that creates an object's prototype.
prototype
The prototype property allows you to add properties and methods
to an object
Date Methods
Here is a list of the methods used with Date and their description.
Date()
Returns today's date and time
getDate()
Returns the day of the month for the specified date according to
local time.
getDay()
Returns the day of the week for the specified date according to
local time.
getFullYear()
Returns the year of the specified date according to local time.
getHours()
Returns the hour in the specified date according to local time.
getMilliseconds()
Returns the milliseconds in the specified date according to local
time.
getMinutes()
Returns the minutes in the specified date according to local
time.
getMonth()
Returns the month in the specified date according to local time.
getSeconds()
Returns the seconds in the specified date according to local
time.
getTime()
Returns the numeric value of the specified date as the number
of milliseconds since January 1, 1970, 00:00:00 UTC.
getYear()
Deprecated - Returns the year in the specified date according to
local time. Use getFullYear instead.
setDate()
Sets the day of the month for a specified date according to
local time.
LN10
Natural logarithm of 10, approximately 2.302.
LOG10E
Base 10 logarithm of E, approximately 0.434.
PI
Ratio of the circumference of a circle to its diameter, approximately 3.14159.
SQRT2
Square root of 2, approximately 1.414.
Math Methods
Here is a list of the methods associated with Math object and their description
abs()
Returns the absolute value of a number.
ceil()
Returns the smallest integer greater than or equal to a number.
cos()
Returns the cosine of a number.
exp()
Returns EN, where N is the argument, and E is Euler's constant, the
base of the natural logarithm.
floor()
Returns the largest integer less than or equal to a number.
log()
Returns the natural logarithm (base E) of a number.
max()
Returns the largest of zero or more numbers.
min()
Returns the smallest of zero or more numbers.
pow()
Returns base to the exponent power, that is, base exponent.
random()
Returns a pseudo-random number between 0 and 1.
round()
Returns the value of a number rounded to the nearest integer.
sin()
Returns the sine of a number.
sqrt()
Returns the square root of a number.
tan()
Returns the tangent of a number.