tags and is run by web browsers. JavaScript allows programming of things like form validation, dynamic updates of content, and interactive effects. It uses variables, functions, and objects to manipulate HTML content and run code on the client-side. The core concepts of JavaScript include variables, data types, operators, conditional statements, loops, and functions."> tags and is run by web browsers. JavaScript allows programming of things like form validation, dynamic updates of content, and interactive effects. It uses variables, functions, and objects to manipulate HTML content and run code on the client-side. The core concepts of JavaScript include variables, data types, operators, conditional statements, loops, and functions.">
JAVASCRIPT Fundamentals
JAVASCRIPT Fundamentals
JAVASCRIPT Fundamentals
INTRODUCTION
JavaScript is a lightweight, interpreted programming language with object-oriented capabilities. The core
of the language has been embedded in the modern web browsers. It now runs inside of database engines,
web servers, game consoles, and refrigerators.
Client-Side JavaScript
Client-side JavaScript is the most common form of the language. The script should be included in or
referenced by an HTML document for the code to be interpreted by the browser. It means that a web page
need not be a static HTML, but can include programs that interact with the user, control the browser, and
dynamically create HTML content. For example, you might use JavaScript to check if the user has entered
a valid e-mail address in a form field.
JavaScript in Web Pages
JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page. If the "src" attribute is present, the <script> element must be empty.
For user agents that cannot or will not handle scripts, authors may include alternate content via
the NOSCRIPT element
Scripts without async , defer or type="module" attributes, as well as inline scripts, are fetched and
executed immediately, before the browser continues to parse the page.
<!DOCTYPE html>
<html>
<head>
<script >
document.write ("Hello World!");
</script>
<body>
</body>
</html>
Execution of JavaScript Programs
Javascript statements between <script></script> tags are executed in the order of appearance. The
execution of scripts occurs as part of web browser’s HTML parsing process. Your scripts should not
attempt to manipulate objects that have not been created.
In the window there is document object the core of the DOM and the html element which is the html tag
and then the children.
5
console.dir(document);
console.log(document.domain);
console.log(document.url);
console.log(document.title);
Comments in JavaScript
//
/* */
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
DataTypes
The type of values that can be represented and manipulated in a programming language are known as
data types. The three primitive data types JavaScript supports are :
Numbers, e.g., 123, 120.50 etc.
Strings of text, e.g. "This text string" etc.
Boolean, e.g. true or false
Note: Java does not make a distinction between integer values and floating-point values. All numbers in
JavaScript are represented as floating-point values. JavaScript represents numbers using the 64-bit
floating-point format defined by the IEEE 754 standard.
Trivial data types : null and Undefined each of which define a single value
Composite data type : object
This is a collection of values
array
Variables
Variables in JavaScript are containers which hold reusable data. Before you use a variable in a JavaScript
program, you must declare it. Variables are declared with the var keyword as follows.
var or let
Storing a value in a variable is called variable initialization. You can do variable initialization at the time of
variable creation or at a later point in time when you need that variable.
JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type.
Unlike many other languages, you don't have to tell JavaScript during variable declaration what type of
value the variable will hold. The value type of a variable can change during the execution of a program
and JavaScript takes care of it automatically.
Variable names should not start with numeral (0-9). They must begin with a letter or an underscore
character. For example, 123test is an invalid variable name but _123test is a valid one. Should not use
any of the JavaScript reserved words.
x = 5; // Assign 5 to x
var x; // Declare x
In ES2015, two other ways to declare variables were introduced. They are let and const
const is declared like var and let.Use const when you are sure a variable will not be redeclared.
1. Double quotes: "Hello".
2. Single quotes: 'Hello'.
3. Backticks: `Hello`.
Double and single quotes are “simple” quotes. There’s no difference between them in JavaScript.
Backticks are “extended functionality” quotes. They allow us to embed variables and expressions into a
string by wrapping them in ${…}, for example:
let name = "John";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, John!
// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3
CONTROL STATEMENTS
Use else if to specify a new condition to test, if the first condition is false
Syntax
while (condition) {
// code block to be executed
}
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump
out" of a switch() statement.
The break statement breaks the loop and continues executing the code after the loop (if any):
Example
for (i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
Example
for (i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
Operators
Objects
Objects are variables too. But objects can contain many values. JavaScript objects are containers
for named values called properties or methods.
Arrays
Date : It stores the date, time and provides methods for date/time management.
For instance, we can use it to store creation/modification times, to measure time, or just to print out the
current date.
Functions
Functions are the main “building blocks” of the program. They allow the code to be called many times
without repetition.
We’ve already seen examples of built-in functions, like alert(message), prompt(message,
default) and confirm(question). But we can create functions of our own as well.
function name(parameter1, parameter2, parameter3) {
// code to be executed
}
Inside the function, the arguments (the parameters) behave as local variables.
A function can return a value back into the calling code as the result.
The simplest example would be a function that sums two values:
function checkAge(age) {
if (age > 18) {
return true;
} else {
return confirm('Do you have permission from your parents?');
}
}
if ( checkAge(age) ) {
alert( 'Access granted' );
} else {
alert( 'Access denied' );
}
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
function doNothing() {
return;
}
document.getElementById(id)
document.getElementsByClassName(className)
document.getElementsByTagName(tagName)
document.querySelector(cssSelector)
document.querySelectorAll(cssSelector)
var salsMotto = document.getElementById("salsMotto");
salsMotto.innerHTML = "Math is cool";
The methods getElementsByClassName and getElementsByTagName return an HTMLCollection
object that acts like an array. That collection is "live", which means the collection is updated if
additional elements with tag name or class name are added to the document.