15 Lecture JavaScript
15 Lecture JavaScript
JavaScript
Felix - IT Systems
JS ( JavaScript ) is Case Sensitive
• Unicode defines the characters and their code points, while UTF-8 is one of
the encoding schemes used to represent those Unicode characters in
binary form.
• UTF-8 is a popular choice for encoding Unicode text due to its efficient use
of space and compatibility with ASCII.
Difference between UTF-8 and Unicode
UTF-8 and Unicode are related but different concepts in the world of character
encoding and text representation.
-> Unicode:
• Unicode assigns a unique numeric code point to each character, which can be
represented as a hexadecimal value (e.g., U+0041 for the Latin letter "A").
• Unicode is a character set, not an encoding. It defines the characters and their
code points, but it doesn't specify how these characters should be represented in
binary form.
-> UTF-8
• UTF-8 (Unicode Transformation Format - 8-bit) is one of several encoding
schemes used to represent Unicode characters in binary form.
• Developer have used different ways of joining multiple words into one
name…
first_name, last_name
FirstName, LastName
firstName, lastName
JavaScript Comments
• Comments can be used to explain JS code.
• Multi-line Comments
Multi-line comments start with /* and end with */.
Variable
JavaScript variables are containers for storing data values.
• Using var
• Using let
• Using const
Rules for Declaring variables
<p id="demo"></p>
<script>
var i = 15;
var j = 10;
var total = i + j;
<p id="p1"></p>
<script>
const x = 15;
const y = 10;
const total = x + y;
document.getElementById("p1").innerHTML = total;
</script>
</body>
Example
<body>
<p id="p1"></p>
<script>
let x = 15;
let y = 10;
let total = x + y;
document.getElementById("p1").innerHTML = total;
</script>
</body>
One Statement Code
• You can declare many variables in one single line.
or
• Use const if the type should not be changed (Arrays and Objects)
Operator Description
+ Addition
- Subtraction
* Multiplication
** Exponentiation
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
Assignment Operators
<script>
var i = 5;
var j = 2;
var value = i * j;
document.getElementById("demo").innerHTML = "Value is " + value;
</script>
Output
Example
<p id="demo"></p>
<script>
var x = 10;
x += 5;
document.getElementById("demo").innerHTML = x;
</script>
Output
Comparison Operators
Operator Description
== equal to
=== equal value and equal type
!= not equal
!== not equal value or not equal type
> greater than
< less than
>= greater than or equal to
<= less than or equal to
? ternary operator
Logical Operators
Operator Description
<p id="demo"></p>
<script>
var x = 10;
x++;
var z = x;
document.getElementById("demo").innerHTML = z;
</script>
Output
JS Data Types
JS variables can hold many data types like
numbers, strings, objects and more…
var x = 16 + “John";
Data Types in JS
<script>
var i = 16; // Number
<p id="demo"></p>
<script>
var first = "Felix";
var second = 'ITs';
document.getElementById("demo").innerHTML =
first + "<br>" +
second;
</script>
Output
JavaScript Arrays
<p id="demo"></p>
<script>
var names = ["John", "Doe", "Andy"];
document.getElementById("demo").innerHTML = names[1];
</script>
Output
JavaScript Objects
<script>
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
Output
Primitives
• A primitive value is a value that has no properties or methods.
• string
• number
• boolean
• null
• undefined
• Symbol etc..
typeof Operator
You can use the JS typeof operator to find the type of a JS variable.
Example
<p id="demo"></p>
<script>
var i = 10;
var name = "Felix";
var j = false;
document.getElementById("demo").innerHTML =
typeof i + "<br>" +
typeof name + "<br>" +
typeof j;
</script>
Output
Function
<script>
function changeText() {
document.getElementById("p1").innerHTML = "Hello from JS";
}
</script>
Output
Why we use functions ?
- Reuse code.
You can define the code once, and use it many times.
Exmaple
<script>
function sum(a,b) {
return document.getElementById("p1").innerHTML = a+b;
}
</script>
Output
Strings
JavaScript strings are for storing and manipulating text.
<p id="demo"></p>
<script>
var string = "Welcome to Felix ITs ";
document.getElementById("demo").innerHTML = string.length;
</script>
Output
String Concate
<p id="demo"></p>
<script>
var s1 = "Hello ";
var s2 = "World"
document.getElementById("demo").innerHTML = s1 + " " + s2;
</script>
Output
Search String
<p id="demo"></p>
<script>
var s1 = "Hello from JavaScript";
var s2 = s1.search('Java');
document.getElementById("demo").innerHTML = s2;
</script>
Output
slice() Method
<p id="demo"></p>
<script>
var s1 = "Hello from JavaScript";
var s2 = s1.slice(6,10);
document.getElementById("demo").innerHTML = s2;
</script>
Replace String
<p id="demo"></p>
<script>
var s1 = "Hello from JavaScript";
var s2 = s1.replace("Hello", "HELLO");
document.getElementById("demo").innerHTML = s2;
</script>
Output
Lowercase, Uppercase
<p id="demo"></p>
<script>
var s1 = "Hello from JavaScript";
var s2 = s1.toLowerCase();
var s3 = s1.toUpperCase();
document.getElementById("demo").innerHTML =
s2 + "<br>" + s3;
</script>
Output
trim() method
<p id="demo"></p>
<script>
var s1 = " Hello from JavaScript ";
var s2 = s1.trim();
document.getElementById("demo").innerHTML = s2;
</script>
Output
Thank You