JAVASCRIPT
Introduction
• JavaScript is the programming language of the Web.
• JavaScript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScript to program the behavior of web pages
• Eg. Js can change :
• HTML content
• HTML attributes
• HTML stles (CSS)
JavaScript Where To
• In HTML, JavaScript code is inserted between <script> and </script> tags.
• You can place any number of scripts in an HTML document.
• Scripts can be placed in the <body>, or in the <head> section of an HTML
page, or in both.
• Scripts can also be placed in external files.
• JavaScript files have the file extension .js.
• To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
JavaScript Output
• JavaScript can "display" data in different ways:
Using innerHTML
Using document.write()
Using window.alert()
• You can use an alert box to display data:
Using console.log()
JavaScript Statements
• A computer program is a list of "instructions" to be "executed" by a computer.
• In a programming language, these programming instructions are
called statements.
• A JavaScript program is a list of programming statements.
• JavaScript statements are composed of:
• Values, Operators, Expressions, Keywords, and Comments.
• This statement tells the browser to write "Hello Dolly." inside an HTML element with
id="demo":
JavaScript Statements
JavaScript Syntax
• JavaScript syntax is the set of rules, how JavaScript
programs are constructed:
JavaScript Values
• The JavaScript syntax defines two types of values:
• Fixed values
• Variable values
• Fixed values are called Literals.
• Numbers are written with or without decimals: 10.50, 100
• Strings are text, written within double or single quotes: "John Doe" , 'John Doe'
• Variable values are called Variables.
JavaScript Variables
• Variables are Containers for Storing Data
• JavaScript Variables can be declared in 4 ways:
• Automatically
• Using var
• Using let
• Using const
JavaScript Variables
JavaScript Variables
JavaScript Let
With let, you cannot do that; With var, you can do that;
Block Scope
• JavaScript had Global Scope and Function Scope.
• JavaScript keywords, var and let provide Block Scope in JavaScript.
• Variables declared inside a { } block cannot be accessed from outside the block:
JavaScript Let
• Redeclaring a variable inside a block will not redeclare the variable outside the
block:
Redeclaring
• Redeclaring a JavaScript variable with var is allowed anywhere in
a program:
Redeclaring
• With let, redeclaring a variable in • Redeclaring a variable with let, in
the same block is NOT allowed: another block, is allowed:
JavaScript Const
JavaScript Data Types
JavaScript has 8 Datatypes
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
The Object Datatype
The object data type can contain:
1. An object
2. An array
3. A date
JavaScript Data Types
The Concept of Data Types
• In programming, data types is an important concept.
• To be able to operate on variables, it is important to know something about the
type.
• Without data types, a computer cannot safely solve this:
• JavaScript will treat the example above as:
Js Arithmetic Operator
Js Assignment Operator
Js Comparison Operator
Let's Apply Javascript in your web page.