Wk 6 JavaScript Part I
Wk 6 JavaScript Part I
JavaScript
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 1 / 23
Introduction About JavaScript
About JavaScript
❂ JavaScript is not Java
✍ The original name for JavaScript was Mocha then LiveScript
✍ It carries the functionality of Scheme language, the object-orientation of Self language, and the
syntax is similar to C and Java. However, JavaScript is a complete, and full-featured language.
✍ Developed by Brendan Eich in 1995, while he was working in Netscape.
✍ The developer later co-founded Mozilla and Brave Software
/* JavaScipt */ /* C */
❂ A Set of languages, where the translator program assigns a type to a variable at runtime based on the
variable’s value at that time.
❂ Data-type is associated with value rather than a variable, giving freedom to the developers to modify
the data type of a variable. Example: JavaScript, Python, Ruby, PHP, Erlang and Perl.
❂ In static typed languages, once the data type of a variable is set, it is not allowed to change.
/* JavaScipt */ /* C */
❂ While inline JavaScript can be quick and easy to use, it is generally not recommended, as it can make
your HTML code harder to read and maintain.
❂ Internal JavaScript: This is the type of JavaScript that is included within the HTML document,
using the <script> tag in the <head> or <body> section of the HTML document. For example:
<script type="text/javascript">
var abc = "Hello World!";
document.write(abd)
</script>
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 8 / 23
Introduction Embedding JavaScript in HTML
❂ Internal JavaScript is a better option than inline JavaScript, as it keeps your JavaScript code
separate from your HTML code, making it easier to read and maintain.
❂ External JavaScript: This is the type of JavaScript that is included in a separate file with a .js
extension, and then linked to the HTML document using the <script> tag with the src attribute.
For example:
<head>
<script src="myScript.js"></script>
</head>
❂ External JavaScript is the best option for larger projects, as it keeps your JavaScript code separate
from your HTML code, and also allows you to reuse the same JavaScript file across multiple HTML
documents.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 9 / 23
Data types Primitive data types
❂ JavaScript has three primitive data types: number, string, and boolean. Everything else is an object
❂ JavaScript is a loosely typed language. That is, explicite declaration of variable types are not
necessary in JavaScript
❂ let abc= "abc"; abc = 123; bcd = 123;
❂ Numbers are always stored as floating-point values. Hexadecimal no. begin with 0x. Some platforms
treat 0123 as octal, others treat it as decimal
❂ let a=123, b=0123, c=0x123; console.log("a="+a+"\nb="+b+"\nc="+c);
❂ Booleans are either true or false. 0, ‘0’, empty strings, undefined, null, and NaN are considered as
false, other values are true
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 10 / 23
Data types First JavaScript Program
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>First JavaScript Program</title>
5 </head>
6 <body onload="confirm(Date())">
7 </body>
8 </html>
Example-1
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script>
5 function myFunction() {
6 let nm = prompt("Enter your name: ");
7 document.getElementById("pID").innerHTML =
8 "Dear " + nm + ", Welcome to the world of JavaScript ";}
9 </script> Dear User, Welcome to the world of
10 </head> JavaScript
Variable Declaration
❂ Using the keyword var
var abc = "abc", bcd, efg;
for(var i = 0; i < 10; i++ ){};
❂ Using the keyword let
let abc = "abc", bcd, efg;
for(let i = 0; i < 10; i++ ){};
❂ What will be the output of the following statement?
let abc; console.log(abc);
❂ Value of the variable is considered as undefined, if it is not initialized, not even a null
❂ null refers the absence of a value. The variable bcd, in the following example does not have any
value, but it will have later on.
let abc = null; console.log(abc);
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 13 / 23
Variable and Constants Naming convention
❂ The type of null variable is object whereas the type of undefined variable is undefined.
let abc= null, bcd; console.log(typeof abc); console.log(typeof bcd);
❂ null variable treated as 0 in an numeric expression whereas undefined variable will be NaN.
let abc= null, bcd;
console.log(abc + 10); console.log(bcd + 10)
[Homework] What will be the output for the following code segments?
function abc(){ function abc(){
console.log("Hello world"); return("Hello world");
} }
console.log(abc()); console.log(abc());
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 16 / 23
Variable and Constants Differences between var and let
Scope of variable defined using var is the function, where it is defined, whereas, the scope
of variable defined using let is the block, where it is defined.
The let keyword is actually coming from ES6 (ECMAScript) environment. Thus, the
concept of block-scope is new to JavaScript introduced in 2015.
A variable is said to be in temporal dead zone from the start of the block until initializa-
tion.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 17 / 23
Variable and Constants Scope of variables
Accessibility of variables
Variables declared with var are accessible in their scope even before they are declared,
whereas, let variables are not initialized until their definition is evaluated. Accessing them
before the initialization will result ReferenceError in case of let and undefined in case of
var.
Re-declaration
❂ Variables declared using var can be re-declared within the scope of declaration, whereas, variables
declared using let can not-be re-declared.
Constant
PI = 3.14159;
TypeError: Assignment to constant variable.
const PI;
SyntaxError: Missing initializer in const declaration
❂ The value of a constant can not be changed but the content of the value can be changed. For
example, if an object is assigned to a constant, the underlying value of that object can be changed.
const ORG = { name: 'IIIT'}; ORG.name = "NIT"; console.log(ORG.name);
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 20 / 23
Variable and Constants Type conversion
❂ Number to string
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 22 / 23
Variable and Constants Type conversion
Comments
✍ Between /* and */
✍ Java’s javadoc comments, /** ... */, are treated just the same as /* ... */ comments; they have
no special meaning in JavaScript
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
nsaharia@iiitmanipur.ac.in JavaScript 23 / 23