JS Notes
JS Notes
JS Notes
Tokens
2.Identifiers : name provided by programmer to the components of js like variable, function, class
etc.
1. V8 for google
3. JavaScriptCore Safari
Characteristics of Javascript
It is purely object oriented language (based on objects for storing most kind of data)
• Line by line execution, like first it will check if it correct it execute and check next line
Javascript is synchronous in nature : single threaded architecture have one stack for execution.
Implementation of JS
1.internal within html document by suing script tag
2.External we need to create one new file extension should be .js and link with script src .
b)Link the js file with html page using src attribute of script tag.
<html>
<head>
<title>js separate</title>
</head>
<body>
<h1>linking js</h1>
<script src="../js/js1.js"></script>
</body>
</html>
Typeof
It is a keyword use as unary operator to identify the type of data.
(typeof 10);
Syntax
typeof value
Program
console.log(typeof "samsung");//string
console.log(typeof `samsung`);//string
console.log(typeof 'samsung');//string
console.log(typeof 1);//number
console.log(typeof null);//object
console.log(typeof undefined);//undefined
console.log(typeof true);//boolean
console.log(typeof false);//boolean
Note
a. The numbers between -( 2^53 -1) and ( 2^53 -1)---number. Eg : 1----number type
2. String
3.Boolean
a. True 1
b. False 0
a. When we declare a variable in js, js engine implicitly assign undefined value to it.
6. Object
Node
Node is a bundle(extra layer) of google v8 engine and built in methods using c++.
Js is not strictly type language as we don’t have to specify what type of data we want to store. It is
dynamic type
language it will understand while execution .It is not necessary to specify type of data during variable
declaration.
We create variable using variable declaration followed by identifier and we can store anything
console.log(a);
let b;
b=20;
console.log(b);
const c=30;
console.log(c);
block scope can be used only inside the block where it is declared, it cannot be used outside.
Note: The variable declared with let and const have block scope.
Eg1:
{
let a=10;
console.log(a);//can be used
console.log(a);//cannot be used
Eg2:
const a=10;
console.log(a);//can be used
Eg3:
var a=10;
console.log(a);//can be used
console.log(a);//can be used
Var
• If we declare a variable using var it has global scope, even inside block it acts has same
let
• A variable creating using let we can use only inside block where it is declared.
1. He can understand html, css behaves like compiler and interpreter which convert all these into
machine language.
4.
5. Every browser will have a js engine to run js code. Therefore the browser become an environment
to run js
It runs js in 2 phases
Understanding execution in js
1. Every time when js engine runs a js code , it will first create a Global Execution context.
Functional area /
Execution area
Typecasting
Implicit typecasting/type conversion : js engine convert one type of data to another type implicitly of
Number zero (0), null , NaN, empty string(''), undefined all these values are considered as false
console.log('hi'?10:20);//
console.log(a);
console.log(b);
console.log(y);
Logical OR
Logical OR behaves differently if LHS value or RHS value is non boolean
Step 2 : if the converted LHS value is true than it returns the original value present in the LHS
Eg
if the converted LHS value is false than it returns the original value present in the RHS
Syntax
Number(data-to-be-converted)
console.log(Number('123')); // 123
ii.If the string consist any other character then we get NaN as output.
a. console.log( Number ('a')); //NaN
Boolean to Number
console.log(Number (20>10)); // 1
Decision Statements
Decision statement helps to skip a block of instruction when we don’t have favoring situation. Eg:
The
instruction of loading home page should be skipped if the entered password is incorrect.
1.If
2.If else
else{
3.Else if
else if(thirdtnumber<fourthnumber)
else {
Switch
Default is optional
Syntax
switch(value)
case value : {
statement;
case value : {
statement;
.
.
default : {
statement;
1.A case blocks gets executed if the value passed to switch matches with value present in case
2.When a case is favorable the case blocks gets executed as well as all the blocks present below in
switch(1)
Break
• It is a control transfer statement.
When a break statement is encountered the control list transferred outside the current
switch or
loop block.
switch(6)
{
break;
break;
Looping
It is also called iteration.
Note: when we design a loop it is a responsibility of a programmer to break the loop after achieving
the desire task, if not the
If we don’t break loop it will be infinite and control will not come out of loop.
Loop statements
1.While
a. Syntax
while(condition){
statement to be repeated;
2.Do-while :
The do-while loop loops through a block of code once, then the condition is evaluated. If the
condition is
Syntax
do{
// statement;
while(condition);
Note:
a. in do while loop the body of the loop is executed first then the condition is evaluated.
b. If the condition evaluated is true the body of the loop inside the do statement is executed again.
d. This process continue until the condition evaluates to false. Then the loop stops.
3. For
4. For-in etc
Functions
Note: in javascript functions are beautiful, every function is nothing but an object.
Syntax to create a function.
2. Function expression.
Function declaration / statement(function statement).
Syntax :
statements;
1.
Note:
i. Function is object
ii. Name of function is variable which holds the reference of function object.
v. when we try to log function name the entire function defination is printed.
console.log('start');
console.log(test);
function test(){
console.log('Hello');
console.log('start');
Function_name(arguments_list,.....);
console.log('start');
function test(){
console.log('Hello');
test();
test();
test();
console.log('start');
console.log('start');
function test(a){
console.log('Hello');
console.log(a);
console.log('start');
Parameters
The parameters have local scope (can be used only inside function body).
Eg:
console.log(a + b)
Arguments
Eg 1:
sum(10,20); // 10,20 are literals used as arguments
Eg 2:
sum(-10+3,-20);//-27
Eg 3:
return
1. It is a keyword used as control transfer statement in a function.
Return will stop the execution of the function and transfer control along with data
to the caller.
2.
function toMeters(cms){
return (cms/100);
console.log(toMeters(cms));
var m =toMeters(cms );
console.log(m);
2. Function as Expression
Syntax :
a();// error
This
Therefore with the help of this variable we can use members of global window object.
Whenever we call a function a new execution context is created. Inside that all local
variable declare inside will be there and 1 more "this" will be there which is different from
this
a.In javascript 'this' is a property of every function.(every function will have this
keyword)
b. Generally this contains the current execution context to which the function belongs.
3.Arrow Functions
• Arrow function was introduce from ES6 OF JS.
Syntax
( parameter_list,...) => {}
Note:
1. Parameter is optional.
console.log(c(10,20));
Correct solution
console.log(c(10,20));
Steps to achieve
Add another pair pf braces next to it which behaves like function call statement.
○ Eg1
(function abc(){
console.log("Hi");
})();
○ Eg2
console.log(a);
5.Anonymous function
The function declare without any name which is called as anonymous function
Syntax:
function (){
instruction
commonly used scripting languages. The term .client-side scripting language means
that it runs at the client-side( or on the client machine) inside the web-browsers,
but one important thing to remember is that client's web-browser also needs to support
the JavaScript or it must be JavaScript enabled. Nowadays, most of the modern web browsers
-Now let's see how the JavaScript engine handles and runs .js code.
-As we already know, JavaScript is an interpreted language that means it gets executed in line by line
manner
(or which means the JavaScript engine converts the Js code line by line and runs in the same manner
instead of converting the whole program once).
Step 1: Parser
-This is the first stage of the engine, every time we run a JavaScript program,
The parser's job is to check the JavaScript code for syntactic errors in line
-Once the parser checks all JavaScript codes and gets satisfied that there are no mistakes/errors in
the code,
it creates the data structure called AST (it stands for Abstract Syntax Tree).
-Once the Abstract Syntax Tree is created by the parser, the JavaScript engine converts the
JavaScript code into the machine code (or in the language that machine can understand).
-When the program written in the JavaScript gets converted in the machine language (or in byte
code),
the converted code is sent to the system for execution, and finally, that byte code run by the
system/engine.