JavaScript Lecture
JavaScript Lecture
JavaScript
What is JavaScript?
JavaScript is a scripting language designed for
Web pages by Netscape. It is a programming
language that is used to enhance HTML pages
and make a website responsive
Why Use JavaScript?
String
• for strings. A string may have zero or more characters, there’s no separate single-character type.
Boolean
• For true/false .
Null
• for unknown values – a standalone type that has a single value null .
Undefined
Objects
• objects are used to store keyed collections of various data and more complex entities.
Bigint
• for integer numbers of arbitrary length and is created by appending n to the end of an integer
JavaScript Data Type Conversion
Results in 3.14
Results in “123”
JavaScript Variables
document.getElementById("demo
").innerHTML = text.length;
</script>
Results in 26
String Slice slice() extracts a part of a string let text = "Apple, Banana, Kiwi";
and returns the extracted part in a
new string. let part = text.slice(7,13);
document.getElementById("demo
").innerHTML = part;
Results in Banana
Formatting String
Strings also has the template literals (backtick) which uses ${} to
include the variable value within the string.
JavaScript has built-in methods for formatting strings. "toFixed()“
which is used to format a number using fixed-point notation. It can
be used to format a number with a specific number of digits to the
right of the decimal.
• "toPrecision()“ which is used is used to format a number to a
specific precision or length.
Formatting String
method description syntax
Lower case string method As the name implies, you const lowercased =
use string.toLowerCase()
the toLowerCase() string
method to convert strings
to their lowercase version.
document.getElementById("d
emo").innerHTML = text;
Iterating Over an Array You can use a for..of loop to const letters = ["a","b","c"];
iterate over the elements of
an Array: let text = "";
for (const x of letters) {
text += x + "<br>";
}
document.getElementById("d
emo").innerHTML = text;
JS Hoisting
JavaScript uses a concept called hoisting, where variable and
function declarations are moved to the top of their scope during
execution. This can cause issues with variable assignment and
function invocation if not understood.
JS Arrow Function
JavaScript introduced arrow functions in ECMAScript 6, which is a
shorthand for defining anonymous functions. They have a shorter
syntax and use the "=>" operator to separate the function
arguments and body.
function example
With Arrow Function let hello = "";
hello = () => {
return "Hello World!";
}
document.getElementById("demo").innerHTML =
hello("Universe!");
Arrow Function Without Parentheses: let hello = "";
document.getElementById("demo").innerHTML =
hello("Universe!");
Introduction to JS Class
JavaScript introduced classes in ECMAScript 6, which is a blueprint
for creating objects. Classes can have properties and methods and
can be used to create objects with similar behavior.
Use the keyword class to create a class and always add a method
named constructor():
Introduction to JS Class
method syntax
JavaScript Class class ClassName {
constructor() { ... }
}