UNIT IV JAVASCRIPT
Introduction,
Language elements,
objects of JavaScript,
other objects like data, math, string, regular expressions, and arrays.
Assigement - 4
1) Explain JavaScript. Why we Use JavaScript?
2) What are the language constructs of JavaScript?
3) Explain object and array in JavaScript.
4) Explain the 8 types of JavaScript.
5) Which symbol is used for comments in Javascript?
6) What is the working of timers in JavaScript?
7) Write about string mainpulators in javascript.
8) Write about functions in javascript.
9) Write about statements in javascript.
10) Write about mathematical function in javascript.
JavaScript
Java Script is one popular scripting language over internet. Scripting means a small sneak (piece). It is always independent on other
languages.
JavaScript is most commonly used as a client side scripting language. This means that JavaScript code is written into an HTML page.
When a user requests an HTML page with JavaScript in it, the script is sent to the browser and it's up to the browser to do something
with it.
Difference between JavaScript and Java
JavaScript Java
Cannot live outside a Web page Can build stand-alone applications or live in a Web page as an applet.
Doesn‘t need a compiler Requires a compiler
Knows all about your page Applets are dimly aware of your Web page.
Untyped Strongly typed
Somewhat object-oriented Object-oriented
There are no relationship between in java & java script. Java Script is a scripting language that always dependent in HTML language. It
used to css commands. It is mainly used to creating DHTML pages & validating the data. This is called client side validations.
Use JavaScript
Using HTML we can only design a web page but you can not run any logic on web browser like addition of two numbers, check any
condition, looping statements (for, while), decision making statement (if-else) at client side. All these are not possible using HTML So
for perform all these task at client side you need to use JavaScript. Features of JavaScript JavaScript is a client side technology, it is
mainly used for gives client side validation, but it have lot of features which are given below;
Features of JavaScript
JavaScript is a client side technology, it is mainly used for gives client side validation, but it have lot of features which are
given below;
JavaScript language elements (JavaScript)
The JavaScript™ language elements are based on the ECMAScript Language Specification Standard ECMA-262 (see
http://www.ecma-international.org/publications/standards/Ecma-262.htm). The JavaScript™ described here applies to the server-
side interpreter. Client-side scripts are interpreted by the browser.
The following table lists the language elements:
Language element Description
Statements (JavaScript) Keywords and syntax that form the framework of a script.
Operators (JavaScript) Symbols and keywords that assign, compare, and combine values.
Top-level functions (JavaScript) Functions not associated with any object.
@Functions (JavaScript) Functions that emulate IBM® Notes® @functions.
Implied variable typing (JavaScript) Ability to associate a variable with a data type to document your code and direct content assist.
Embedding Java in JavaScript Ability to embed Java™ code.
Creating a java script
Html script tag is used to script code inside the html page.
<script> </script>
The script is containing 2 attributes . They are
1) Language attribute:- It represents name of scripting language such as JavaScript, VbScript.
<script language =-JavascriptII>
2) Type attribute: - It indicates MIME (multi purpose internet mail extension) type of scripting code. It sets to an alpha-numeric MIME
type of code
<script type=-text/JavascriptII>
Location of script or placing the script
Script code can be placed in both head & body section of html page. Script in head section Script in body section
JavaScript Objects
a) Object Definitions e) Object Accessors i) Object Sets
b) Object Properties f) Object Constructors j) Object Maps
c) Object Methods g) Object Prototypes k) Object Reference
d) Object Display h) Object Iterables
In JavaScript, almost "everything" is an object.
Booleans can be objects (if defined with the new keyword)
Numbers can be objects (if defined with the new keyword)
Strings can be objects (if defined with the new keyword)
Dates are always objects
Maths are always objects
Regular expressions are always objects
Arrays are always objects
Functions are always objects
Objects are always objects
All JavaScript values, except primitives, are objects.
JavaScript Primitives
A primitive value is a value that has no properties or methods. 3.14 is a primitive value
A primitive data type is data that has a primitive value.
JavaScript defines 7 types of primitive data types:
Examples
string
number
boolean
null
undefined
symbol
bigint
Object Properties
The named values, in JavaScript objects, are called properties.
Propert Value
y
firstNam John
e
lastName Doe
age 50
eyeColor blue
Objects written as name value pairs are similar to:
Associative arrays in PHP
Dictionaries in Python
Hash tables in C
Hash maps in Java
Hashes in Ruby and Perl
JavaScript Math Object
Javascript Math object is used to perform mathematical operations on numbers. All the properties of Math are static
and unlike other objects, it does not have a constructor.
We use Math only on Number data type and not on BigInt
Example 1: This example uses math object properties to return their values.
console.log("Math.LN10: " + Math.LN10);
console.log("Math.LOG2E: " + Math.LOG2E);
console.log("Math.Log10E: " + Math.LOG10E);
console.log("Math.SQRT2: " + Math.SQRT2);
console.log("Math.SQRT1_2: " + Math.SQRT1_2);
console.log("Math.LN2: " + Math.LN2);
console.log("Math.E: " + Math.E);
console.log("Math.PI: " + Math.PI);
Output
Math.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.E: 2.71828...
JavaScript String
JavaScript String Object is a sequence of characters. It contains zero or more characters within single or double
quotes.
Syntax:
const string_name = "String Content"
or
const string_name = new String("String Content")
Example: In this example, we will create a string by using the native way and using String Constructor.
// Create a variable and assign String value
const str1 = "First String Content";
console.log("String 1: " + str1);
// Creating a String using String() Constructor
const str2 = String("Second String Content");
console.log("String 2: " + str2);
Output
String 1: First String Content
String 2: Second String Content
Note: If we used String() Constructor then a number can be a string as well.
JavaScript RegExp(Regular Expression)
A regular expression is a sequence of characters that forms a search pattern. Regular expressions can be used to
perform all types of text search and text replacement operations
A regular expression can be a single character or a more complicated pattern.
Syntax:
/pattern/modifiers;
Example:
let patt = /GeeksforGeeks/i;
Explanation :
/GeeksforGeeks/i is a regular expression.
GeeksforGeeks is the pattern (to be used in a search).
i is a modifier (modifies the search to be Case-Insensitive).
Regular Expression Modifiers can be used to perform multiline searches which can also be set to case-
insensitive matching:
Expressions Descriptions
g Find the character globally
i Find a character with case-insensitive matching
m Find multiline matching
Regular Expression Brackets can Find characters in a specified range
Expressions Description
[abc] Find any of the characters inside the brackets
[^abc] Find any character, not inside the brackets
[0-9] Find any of the digits between the brackets 0 to 9
[^0-9] Find any digit not in between the brackets
(x | y) Find any of the alternatives between x or y separated with |
Regular Expression Metacharacters are characters with a special meaning:
Metacharacter Description
\. Search single characters, except line terminator or newline.
\w Find the word character i.e. characters from a to z, A to Z, 0 to 9
Metacharacter Description
\d Find a digit
\D Search non-digit characters i.e all the characters except digits
\s Find a whitespace character
\S Find the non-whitespace characters.
\b Find a match at the beginning or at the end of a word
\B Find a match that is not present at the beginning or end of a word.
\0 Find the NULL character.
\n Find the newline character.
\f Find the form feed character
\r Find the carriage return character
\t Find the tab character
\v Find the vertical tab character
\uxxxx Find the Unicode character specified by the hexadecimal number xxxxx
Regular Expression Quantifiers are used to define quantitiesoccurrence
Quantifier Description
n+ Match any string that contains at least one n
n* Match any string that contains zero or more occurrences of n
n? Match any string that contains zero or one occurrence of n
m{X} Find the match of any string that contains a sequence of m, X times
m{X, Y} Find the match of any string that contains a sequence of m, X to Y times
m{X,} Find the match of any string that contains a sequence of m, at least X times
m$ Find the match of any string which contains m at the end of it
^m Find the match of any string which contains m at the beginning of it
?!m Find the match of any string which is not followed by a specific string m.
Regular Expression Object Properties:
Property Description
constructor Return the function that created the RegExp object’s prototype
global Specify whether the “g” modifier is set or not
ignorecase Specify whether the “i” modifier is set or not
lastindex Specify the index at which to start the next match
multiline Specify whether the “m” modifier is set or not
source Return the text of RegExp pattern
Regular Expression Object Methods:
Method Description
compile() Used to compile the regular expression while executing of script
exec() Used to test for the match in a string.
test() Used to test for a match in a string
toString() Return the string value of the regular expression