JavaScript Core
JavaScript Core
A script is program code that doesn’t need pre-processing (e.g. compiling) before being run. In
the context of a Web browser, scripting usually refers to program code written in JavaScript that
is executed by the browser when a page is loaded, or in response to an event triggered by the
user.
Types of script: Scripts are classified into the following two types.
⮚ Client-side script
⮚ Server-side script
Client-side script:
🡺These scripts are getting executed within the web Browser (client).
🡺Here we don’t need any software.
🡺These scripts are used for client-side validations (data verification & data validations)
Ex: JavaScript, VBScript, typescript etc…
Server-side script:
🡺A script which executes in server machine with support of the web-server/app-server
software’s like IIS(Internet information services), Tomcat, JBOSS, etc.
🡺 These scripts are used for server-side validations (authentication & authorization).
Ex: php, jsp, asp.net, VueScript, Express Script, nodeJS, cgi, perl etc…
And lightweight
Ex: JavaScript, VBScript,TypeScript, Perl, Shell etc. Ex: C, CPP, vb.net, Java etc.
JavaScript 6 Introduction
When JavaScript was created, it initially had another name: “LiveScript”. But Java
was very popular at that time, so it was decided that positioning a new language
as a “younger brother” of Java would help.
But as it evolved, JavaScript became a fully independent language with its own
specification called ECMAScript, and now it has no relation to Java at all.
ES JS Programmer
JS Parser:
JS code (high) JS parser machine code
JS Engine:
V8 Chrome, Edge and Opera
SpiderMonkey Firefox
Chakra IE
SquirrelFish Safari
JS used web dev, mobile app, gaming, animations, networking app, AI...
Using HTML/CSS, we can only design a web page but it’s not supported to perform
logical operations such as calculations, decision making and repetitive tasks,
dynamically displaying output, reading inputs from the user, and updating content
on webpage at client side. Hence to perform these entire tasks at client side we
need to use JavaScript.
Where it is used?
There are so many web applications running on the web that are using JavaScript like
Google, Facebook, twitter, amazon, YouTube etc.
It is used to create interactive webpages.
JS used to add functionality to webpages
It is mainly used for:
1. Client-side verifications and validations
2. Dynamic drop-down menus
3. Displaying date and time
4. Build forms that respond to user input without accessing a server.
5. Displaying popup windows and dialog boxes (like alert dialog box, confirm dialog
box and prompt dialog box)
6. Manipulate HTML "layers" including hiding, moving, and allowing the user to
drag them around a browser window.
etc...
Limitations of JavaScript
>internal scripting
Internal script is nothing but html code and javascript code both are placed in the same file, but
not in the same line.
Internal script must be implemented inside <script> tag, <script> is a paired tag.
> scripting in head sec
head is the first executed part of html, hence javascript is also executed first.
<head>
<script type="text/javascript”>
JS code
</script>
</head>
> scripting in body sec
body level script is executed after head section
<body>
<script type="text/javascript”>
JS code
</script>
</body>
Calling: fun-name();
{
Steps
}
Calling Syn:
fun-name(); html inline or internal
Comments in JavaScript
Comment is nothing but it is a statement which is not displayed on the browser
window. It is useful to understand which code is written for what purpose.
Comments are useful in every programming language to deliver messages. It is used
to add information about the code, warnings or suggestions so that the end user or
other developer can easily interpret the code.
Types of Comments:
There are two types of comments are in JavaScript
1. Single-line Comment ex: // comment
2. Multi-line Comment ex: /* comments */
JS Printing methods
write() method: The write() method writes HTML expressions or javascript
code to a document without line breaking.
Syn: window.document.write(val1, val2, val3….);
writeln() method: The writeln() method writes HTML expressions or
javascript code to a document with line breaking.
Syn: window.document.writeln(val1, val2, val3….);
log() method: The log() method writes HTML expressions or javascript code
on browser’s console (press F12 key) with line break.
Syn: window.console.log(val1, val2, val3…);
JS Naming Conventions
JS => mixed case
⇨ Naming conventions means where we have to use uppercase and where we
have to use lowercase
⇨ While working/using predefined items we must follow these guidelines.
Variables are used to store/to hold a value for reuse purpose and automatically
substitute values in steps.
We can declare variables in open script tag (global), within function (local) or within
block (block level).
<script> function fun1() for( ; ; ){
var x; { var z;
</script> var y; }
}
for example:
var eid; var 1a;
var total; var a1;
var _b; var book id;
var a@; var studentid;
var #b; var case;
var book_id; var a$1
Loosely Typed
Java script did not provide any data types for declaring variables and a variable in
javascript can store any type of value. Hence java script is loosely typed.
dynamically typed
We can use a variable directly without declaring it in javascript, it’s called dynamic
typed programming.
Var Let Const
Global Variable
var is declared within the script tag but outside function & block those are global
variables.
These global variables are accessible from anywhere in the program.
Declared with a window object is known as a global variable.
JavaScript datatypes:
In JavaScript data types are classified into the following two cat.
1. Primitive datatypes
2. Non-primitive datatypes
[] { }
var name="nit";
var name='nit';
Number: Javascript has only one type of numbers,they can be return with or without
decimals
var x1=34.00; with decimals
typeof
typeof is one of reversed word, it's used to identify datatype of a variable or value.
typeof value
Non-primitive data types: When a variable is declared with the keyword new, the
variable is an object.
new is used for dynamic memory allocations (for creating objects and arrays).
Ex:
var st=newString();
var x=newNumber();
let y=newBoolean();
let a = [ ];
JavaScript Operators
Operator is a symbol (special char) and it is used to perform certain operation (task).
Ex:
Expression
Operator Categories
1. Unary it requires one operand
o increment
o decrement
Special operators:
Arithmetic operators: using these operators we can perform the basic math
calculations.
Ope are + - * / % **
operators are:
operator Description example
+ addition j+12
- subtraction j-22
* multiplication j*7
/ division j/3
% modulus j%6
** power x**y xy
relational operators: these operators are used to provide comparison between two
operands. These are boolean operators (true/false).
== is equal to j==42
=== a===b
!== a!==b
|| OR j<100 || j>0
! Not !(j==k)
And Or Not
Cond1 Cond2 Result Cond1 Cond2 Result Cond Result
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F
Operator is =
Total=total+price è total+=price
= store a=10
shorthand:
+= addition & assigna+=10
-= subtract & assign a-=5
*= product & assigna*=20
/= division & assign a/=7
%= modulus & assign a%=6
Operator is +
ternary operator: this operator is used for decision making operations. operator is
?:, this operator is also called a conditional operator.
The operator precedence table can help one know the precedence of an operator
relative to other operators.
1 () Grouping –
3 ++ Postfix increment –
-- Postfix decrement – i–
/ Division 18/9
% Remainder 4%2
– Subtraction 4-2
in In “PI” in MATH
!= Inequality x!=y
+= x+=3
-= x-=3
*= x*=3
/= x/=3
%= x%=3
<<= x<<=2
>>= x>>=2
>>>= x>>>=2
&= x&=y
^= x^=y
|= x|=y
1. Alert box
2. Confirm box
3. Prompt box
Alert box:
An alert box is often used if you want to make sure information comes through the
user. When an alert box pops up, the user will have to click "ok" to proceed.
Syn: window.alert("message"/expr);
Confirm box:
It is often used, if you want the user to verify and accept something. When a confirm
box pops up, the user will have to click either "ok" or "cancel" to proceed. If the user
clicks "ok" the box returns “true”. If the user clicks "cancel" the box returns "false".
Prompt Box:
It is used to, if you want the user to input a value while entering a page. When a
prompt box pops up the user will have to click either "ok" or "cancel" to proceed
after entering an input value. If the user clicks "ok" the box returns the value/empty.
If the user clicks "cancel" the box returns "null".
Dynamic
o While execute of program(after webpage open) assigning values to
vars
o This given by user
o This always changing, means data changing the data execution to
execution
o We can take the data from user, in two ways:
§ Html input elements (UI/html forms)
§ Prompt dialog
Note:
Parsing
Changing Data from string format to number format (actual data type)
parseFloat()
predefined function of window, used string based number converts into
floating type.
Syn: window.parseFloat("value")
“100" => 100
"10.78" => 10.78
"rama" => NaN (Not a Numeric)
Control Statement
Control statements are used to control (change) execution order of a program based
on user input data.
Types:
Conditional Statements:
If Statement
· simple if
· If else
If statement
<script>
var a=10;
if(a>5)
</script>
if-else statement
In general, it can be used to execute one block of statements among two blocks.
Example of ifelse statement
<script>
var a=40;
if(a%2==0)
else{
</script>
Result
a is even number
It evaluates the content only if the expression is true from several expressions.
Syntax
if(expression1)
else
if(expression2)
else
}
Example of if..else if statement
<script>
var a=40;
if(a==20)
else if(a==5)
else if(a==30)
else
</script>
switch statement
> switch is a selection statement, but it's not decision making.
> its better performance.
Syn:
switch(var/expr)
{
case value: statements...
break;
case value: statements...
break;
case ...
default: statements...
}
Looping Statements
Set of instructions given to the interpreter to execute until the condition becomes
false is called loops. The basic purpose of the loop is min code repetition.
The way of the repetition will be forming a circle that's why repetition statements
are called loops. Some loops are available In JavaScript which are given below.
· for loop
while loop
When we are working with “while loop” always pre-checking process will be
occurred. Pre-checking process means before evolution of statement block condition
part will be executed. “While loop” will repeat in clockwise direction or anticlockwise
direction.
Syn:-
initval;
while(condition)
{
Statements
Stepping
}
<script>
var i=10;
while (i<=13)
document.write(i + "<br/>");
i++;
}
</script>
do-while loop
<script>
var i=11;
do{
document.write(i + "<br/>");
i++;
}while (i<=15);
</script>
for Loop
For loop is a simplest loop first we initialized the value then check condition and then
increment and decrements occurred.
<script>
document.write(i + "<br/>")
</script>
Unconditional statements
Types:
Øbreak
Øcontinue
Ø return
<noscript> tag: It is used to provide an alternate container for users when script is
disabled or not supported, It is a paired tag. It is always declared within the body
section.
syntax: <noscript>------</noscript>
ex:
<head>
<script type='text/javascript'>
alert("welcome to js");
</script>
</head>
<body>
<noscript>
</noscript>
</body>