Javascript: by - Y.Poojitha M.Manjulatha
Javascript: by - Y.Poojitha M.Manjulatha
Javascript: by - Y.Poojitha M.Manjulatha
By –
Y.Poojitha
M.Manjulatha
In 1995
What is JavaScript?
Javascript is an object based, client side scripting
language which is used for creating dynamic web pages
in web applications.
</body>
</html>
Output:
2.JavaScript in <body>...</body> Section
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Hello World")
//-->
</script>
</body>
</html>
Output:
3.JavaScript in <body> and <head> Sections
<html>
<head>
<script type="text/javascript">
<!--
function sayHello() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Hello World")
//-->
</script>
<input type="button" onclick="sayHello()" value="Say Hello"
/>
</body>
</html>
Output:
Simple example for internal JS:
<html>
<head>
<title>Internal JavaScript</title>
<script type="text/javascript">
document.write("This is Internal Javascript");
</script>
</head>
<body></body>
</html>
Output:
This is Internal Javascript
Simple example for external JS:
<html>
<head>
<title>External JavaScript</title>
<script type="text/javascript” src="external.js">
</script>
</head>
</html>
external.js
document.write("This is External Javascript");
Regular
ex
Primitive data types:
Specify the size and type of variable values. They are the building
blocks of data manipulation and cannot be further divided into simpler
data types.
Primitive data types are number, string, boolean, NULL, Infinity and
symbol.
Example: var a = 5;
The variable ‘a’ refers to a single value in memory. If we want to change the value of a,
we would have to assign a new value to a.
When we create a variable, it reserves a space for itself in the memory.
The variable ‘a’ has space in memory which holds its value. When we try to change
the value of ‘a’ by assigning another value like var a = 6, it doesn’t alter the value of
the original a, it just creates a new variable ‘a’ with the new value 6.
var a1=a;
Here the variable ‘a1’ is assigned the value of ‘a’, not the address of ‘a’ in memory.
JavaScript Variable
A JavaScript variable is simply a name of storage location.
Example:
var a =5;
var b=6;
a==b // returns false
Or
var check = Boolean(expression); // If the expression evaluates to true, the value of
(a==b) ‘check’ will be true or else it will be false.
Example 2:
var mystring = ‘hi there’;
Boolean(mystring); // This will result in true because the ‘mystring’ value exists
Strings:
The string data type in JavaScript can be any group of characters enclosed by a single or
double-quotes or by backticks.
var str1 = “This is a string1”; // This is a string primitive type or string literal
var str2= ‘This is a string2’;
Null:
The null in JavaScript is a data type that is represented by only one value, the ‘null’
itself. A null value means no value.
var a = null;
document.write (a); // This returns null
typeof(a); // This returns object
This means the type of a null value is an object, not null.
Arrays:
An array in JavaScript is an object data type. An array contains more than one value
with a numerical index, where the index starts from 0. Thus it holds its value in a key-
value pair.
var arr1= [1, 2, 3];
We cannot mutate or change the above array arr1 .
arr1[0] =4;
document.write(arr1) // This will return the array [4, 2, 3]
typeof (arr1) // will return the data type ‘object’.
The array ‘arr1’ refers to the address in memory which contains the value [4, 2, 3].
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.“;
</script>
</body>
</html>
<p id="demo"></p>
<script> Output:
const person = {
Sathvika is 100 years old.
firstName: "Sathvika",
lastName: "Kammampati",
age:50,
eyeColor: "blue"
};
const x = person;
x.age = 100;
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
Example:
OUTPUT:
JavaScript Scope of Variables
Scope determines the accessibility (visibility) of variables.
JavaScript has 3 types of scope:
• Block scope
• Function scope
• Global scope
Block Scope
ES6 introduced two important new JavaScript keywords: let and const.
These two keywords provide Block Scope in JavaScript.
Variables declared inside a { } block cannot be accessed from outside the block:
Since local variables are only recognized inside their functions, variables with the same name can be used in
different functions.
Local variables are created when a function starts, and deleted when the function is completed.
Function Scope
• JavaScript has function scope: Each function creates a new scope.
• Variables defined inside a function are not accessible (visible) from
outside the function.
• Variables declared with var, let and const are quite similar when declared
inside a function.
A global variable has Global Scope:
All scripts and functions on a web page can access it.
Global Scope
Variables declared Globally (outside any function) have Global Scope.
Global variables can be accessed from anywhere in a JavaScript program.
Variables declared with var, let and const are quite similar when declared
outside a block.
THANK YOU