JavaScript
Scripting language for web developing
Used to develop server side programs (Node) as well
Object Based Programming Language
Use script tag to write JS code in html page
Loosely typed language
Built-in Objects
console
o represents the web console (terminal)
o use log method to write output on console
window
Built-in Values
NaN
o Not a Number
o Is of type number
o E.g.
[Link](parseInt(“test”));
Infinity:
o When a number is divided by 0
o E.g.
answer = 10 / 0; // Infinity
undefined:
Variables
To declare a variable data type MUST NOT be used in its declaration
Syntax:
o <var nam> = <initial value>;
E.g.
o num = 100; // number
o salary = 4.5; // number
o name = “test”; // string
o firstName = ‘steve’; // string
o canVote = true; // Boolean
Variable Scope
global
o a variable declared outside any function
o can be declared with or without var keyword
o can be accessed outside or inside any function
o E.g.
num = 100;
var salary = 4.5;
o can be declared inside a function without using a var keyword
o E.g.
function function1() {
// global
firstName = “test”;
}
Local
o Must be declared inside a function with keyword var
o Can NOT be accessed outside the function in which it is
declared
o E.g.
function function1() {
// local
var firstName = “test”;
}
Function
Function must be declared along with its definition
To declare a function, function keyword is used
Syntax:
function <function name> ( <params> ) {
// function body
}
Properties
o A function CAN NOT decide the data type of parameters
o Only caller decides the data type of parameters
E.g.
function function1 (p1) {
[Link](“p1 = ” + p1 + “ type = “ +
typeof(p1));
}
function1(10); // number
function1(“test”); // string
function1(true); // boolean
o If a program contains multiple functions with same name then
only bottom-most function’s definition will be used
E.g.
function function1 () {
[Link](“function1 – 1”);
}
function function1 () {
[Link](“function1 – 2”);
}
function function1 () {
[Link](“function1 – 3”);
}
// output: function1 - 3
function1();
o A function can be called with excess number of parameters
Excess parameters can be used by using a hidden
parameter arguments
E.g.
function function1 (p1, p2) {
// body
}
function1(10, 20, 30, 40, 50); // p1 = 10, p2 = 20
o A function can be called with less number of parameters
E.g.
function function1 (p1, p2) {
// body
}
function1(); // p1 = undefined, p2 = undefined
function1(10); // p1 = 10, p2 = undefined
function1(10, 20); // p1 = 10, p2 = 20
o A function can be called before its declaration
E.g.
function1();
function function1() {
// body
}
Data Types
In JS, all Data Types are inferred (automatically decided by JS …..)
Types
o number:
It supports both whole and decimal numbers
E.g.
num = 100;
salary = 4.5;
o string:
collection of characters
E.g.
firstName = “steve”;
lastName = ‘Jobs’;
o boolean:
may have only true or false value
E.g.
canVote = true;
canVote = false;
o undefined:
o object:
Array
collection of objects
Object
collection of properties (data members or fields) and methods
To create an object (instance)
o Use Object
o Use constructor function
o Use JSON
o Use class (TypeScript)
Using Object
o Object is a built-in function