UNIT1 JavaScript
UNIT1 JavaScript
JavaScript
(Up to
Variables)
Introduction to JavaScript
• JavaScript is a scripting language that is used to create and manage dynamic web pages, basically anything that
moves on your screen without requiring you to refresh your browser.
• When most people get interested in web development, they start with good old HTML and CSS.
• From there, they move on to JavaScript, which makes sense, because, these three elements together form the
backbone of web development.
• HTML is the structure of your page like the headers, the body text, any images you want to include.
• CSS controls how that page looks (it’s what you’ll use to customize fonts, background colors, etc.).
• JavaScript is the third element. Once you’ve created your structure (HTML) and your aesthetic vibe (CSS),
JavaScript makes your site dynamic (automatically updateable).
Why JavaScript?
• JavaScript is an essential programming language, almost compulsory to learn for students or software developers
that are gravitated towards web development.
1. Javascript is the most popular programming language in the world and that makes it a default choice for web
development.
2. JavaScript offers lots of flexibility. We can create stunning and fast web applications with tons of customizations to
provide users with the most relevant graphical user interface.
3. JavaScript is now also used in mobile app development, desktop app development, and game development.
4. The incredible thing about Javascript is that we can find tons of frameworks and libraries already developed, which
can be used directly in web development. That reduces the development time and enhances the graphical user
interface.
What is JavaScript?
• JavaScript is lightweight and interpreted, which makes it much swifter than other languages.
• It is an interpreted, full-fledged programming language that enables dynamic interactivity on websites when
applied to an HTML document.
• It was introduced in the year 1995 for adding programs to the webpages in the Netscape Navigator browser. Since
then, it has been adopted by all other graphical web browsers.
• With JavaScript, users can build modern web applications to interact directly without reloading the page every time.
The traditional website uses js to provide several forms of interactivity and simplicity.
What is JavaScript Used For?
JavaScript is used in various fields from the web to servers, and here’s a quick list of the significant areas it’s
used in:
1. Web Applications: JavaScript is used for adding interactivity and automation to websites.
2. Mobile Applications: JavaScript isn’t just for developing web applications; it is also used for developing
applications for phones and tablets.
• With frameworks like React Native, we can develop full-fledged mobile applications with all those fancy
animations.
3. Web-based Games: If we’ve ever played a game directly on the web browser, JavaScript was probably used to
make that happen.
4. Back-end Web Development: JavaScript has traditionally been used for developing the front-end parts of a
web application. However, with the introduction of NodeJS, a prevalent back-end JavaScript framework, things
have changed. And now, JavaScript is used for developing the back-end structure also.
Say “Hello World” in JavaScript
• Here’s how you can write a simple JavaScript code that prints Hello World on the browser window.
<html> OUTPUT:
Hello World!
<body>
document.write("Hello World!");
</script>
</body>
</html>
1. JavaScript can be easily integrated into the HTML code, and that’s exactly what’s happening here.
2. For those of you not familiar with HTML, this code might seem a bit unfamiliar. But it is perfectly understandable
even to the web development beginners.
3. <html> tag is required to define a HTML web page. <head> tag. The web page contents are defined inside the
<body> tag.
4. Any JavaScript that you add to the web page has to be within the <script> tag.
5. document.write is a JavaScript function that writes the contents onto the web page. In this case, it prints “Hello
World!” on screen.
While HTML and CSS are absolutely required to code a basic web page, JavaScript is the language that will help you
bring that page to life, making it more attractive to the audience. Taking the time to learn JavaScript will help you in
the web development world.
Features of JavaScript
• All popular web browsers support JavaScript as they provide built-in execution environments.
• JavaScript follows the syntax and structure of the C programming language. Thus, it is a structured programming
language.
• JavaScript is a weakly typed language, where certain types are implicitly cast (depending on the operation).
• JavaScript is an object-oriented programming language that uses prototypes rather than using classes for inheritance.
• It is a case-sensitive language.
• Client-side validation,
• Displaying pop-up windows and dialog boxes (like an alert dialog box, confirm dialog box and prompt
dialog box),
2. global variable.
• There are some rules while declaring a JavaScript variable (also known as identifiers).
• After first letter we can use digits (0 to 9), for example value1.
• JavaScript variables are case sensitive, for example x and X are different variables.
Correct JavaScript variables
1.var x = 10;
2.var _value="Hello";
OUTPUT:30
JavaScript local variable
For example:
<script>
function abc(){
<script>
var x=10;//local variable
If(10<13){
} var y=20;//JavaScript local variable
</script> }
Or, </script>
JavaScript global variable
• A variable i.e. declared outside the function or declared with window object is known as global variable.
For example:
<html> OUTPUT:
<body>
<script> 200 200
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
</body>
</html>
JavaScript Identifiers
JavaScript variables must have unique names. These names are called Identifiers.
Note
• The JavaScript var statement declares variables with function scope or globally. Before ES6, var was the sole
keyword for variable declaration, without block scope, unlike let and const.
Syntax:
Function Scope
• The variables declared inside a function are function-scoped and cannot be accessed outside the function.
• The variables declared using the var statement are hoisted at the top and are initialized before the execution of
code with a default value of undefined.
• The variables declared in the global scope that is outside any function cannot be deleted
• In JavaScript, we can declare a variable multiple times using the var keyword without causing an error.
• This is because var has function scope or global scope, depending on where it's declared, and does not
have block scope.
• When we redeclare a variable using var, it doesn't throw an error, and the variable will simply get
reassigned to the new value.
• This can lead to unexpected behavior and is one of the reasons why let and const were introduced.
• Using var we can use it in 4 ways as four cases:
1. we will declare a global variable and access it anywhere inside the program
var test = 12
function f(){
console.log(test);
f();
output:12
2. we will declare multiple variables in a single statement
Output:
var test1 = 12,
12 14 16
test2= 14,
test3 = 16
function fun(){
console.log(test1, test2, test3);
}
fun();
3. we will see the hoisting of variables declared using var
Output:
console.log(test);
undefined
var test = 12;
Explanation: We get the output without any error because the variable test is hoisted at the top even before the execution of the
program began and the variable is initialized with a default value of undefined
Output:
var test = 12;
100
var test = 100;
console.log(test);
Explanation: We did not get any error when redeclaring the variable if we did the same with the let keyword an error would be
thrown
2. JavaScript Let
• The let keyword in JavaScript is used to make variables that are scoped to the block they’re declared in.
• Once you’ve used let to define a variable, you cannot declare it again within the same block. It’s important to declare let variables
before using them.
• The let keyword was introduced in the ES6 or ES2015 version of JavaScript.
• It’s usually recommended to use let when you’re working with JavaScript.
Syntax:
The variables which are declared inside the { } block are known as block-scoped variables. variables declared by the var keyword
cannot be block-scoped.
Example: In this example, the num variable is block-scoped and it cannot be accessed outside the block. If we try to access the
variable outside the block it throws a reference error.
Output:
{
10
let num = 10; Uncaught ReferenceError: num is not defined
• A global scope variable is a variable declared in the main body of the source code, outside all functions.
• Example: In this example, the num variable is a globally scoped variable and it can be accessed from anywhere in the progra
Output:
let num = 10;
10
console.log(num); 10
function fun() {
console.log(num);
}
• A function scope variable is a variable declared inside a function and cannot be accessed outside the function.
• Example: In this example, the num variable is declared inside the function and cannot be accessed outside the function.
Output:
function fun() {
10
let num = 10; "ReferenceError: num is not defined
console.log(num);
}
console.log(num);
1. Redeclaring Variables in different blocks
• The variables declared using let can be redeclared inside other blocks.
Output:
let x = 77;
23
77
{
let x = 23;
console.log(x);
}
console.log(x);
2. Redeclaring Variables in the same blocks
• We cannot redeclare variables using the let keyword inside the same blocks. It will throw an error.
Output:
let x = 77;
Uncaught SyntaxError: Identifier 'x' has already been
declared
{
let x = 23; // legal
console.log(x);
}
console.log(x);
3. JavaScript Const
• The const keyword in JavaScript is used to define variables that cannot be changed once they’re assigned a value. This prevents
any modifications to the variable’s value.
• Additionally, const doesn’t allow redeclaration of the same variable within the same block, and it provides block scope. It was
introduced in ES2015 (ES6) for creating immutable variables.
Output:
Syntax:
Uncaught SyntaxError: Identifier 'x' has already been
declared
const const_name;
const x;
SUMMARY OF VARIABLE DECLARATIONS (var,let & const)
Declaration and
var a=10; let b=20; const auth=”Herb”;
initialization