Java script
Brendan Eich
JavaScript was invented by Brendan Eich in 1995. It was developed for Netscape 2, and became the ECMA-262 standard in 1997. After Netscape handed JavaScript over to ECMA, the Mozilla foundation continued to develop JavaScript for the Firefox browser.
What is Javascript
There are at least three great things about JavaScript:
Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.
The browser has an embedded engine sometimes called a “JavaScript virtual machine”.
Different engines have different “codenames”. For example:
Javascript used for
Front-end (Client side)
Back-end (Server-side)
JavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag
<script> alert( 'Hello, world!' ); </script>
We can write 2 types of Js
Naming convetions
As we’ll be using the browser as our demo environment, let’s see a couple of functions to interact with the user: alert, prompt and confirm.
Javascript Variables
A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.
We have use these variable name to initialise a variable with value.
There are 8 basic data types in JavaScript.
There are 7 Primitive data types
One non-primitive data type:
object for more complex data structures.Type conversions
1. Implicit conversion
2. Explicit conversion
We can convert data types from one data type to another data type is called to type convertion
+,-,*,/,%,**.Js supports this math operators
a > b, a < b.a >= b, a <= b.a == b, please note the double equality sign == means the equality test, while a single one a = b means an assignment.≠, but in JavaScript it’s written as a != b.Even we can perform the string comparisons:
Conditional branching: if, '?'
if statement and the conditional operator ?, that’s also called a “question mark” operator.if(condition){console.log(true)}
if(condition){
} else{
}
if(){
}else if(){
}
'?' This is question mark is called Ternary operator works like if condition
Variables scopes
Scope determines the accessibility (visibility) of variables.
JavaScript has 3 types of scope:
There are 3 logical operators in JavaScript:
&& (AND)! (NOT), Logical operators
Loops in Javascript
We have 3 types of loops :
Functions
We have 3 types of function declarations in JS
Objects
Object is a combination of key value pairs
Ex: const user = {
name: 'R15',
company: 'Yamaha'
}
Object Notations
For accessing the values from the Object we have 2 types of notations
We have a loop for access the object key in JS. That loop is called "for..in" loop
Object methods, "this"
Object.keys()
Object.values()
Object.entries()
' this ' keywords refers to the current object to access the keys.
Constructor, operator "new"
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
We can create an objects with ' new ' keyword constructor
\n |
New line |
\r |
In Windows text files a combination of two characters \r\n represents a new break, while on non-Windows OS it’s just \n. That’s for historical reasons, most Windows software also understands \n. |
\', \", \` |
Quotes |
\\ |
Backslash |
\t |
Tab |
\b, \f, \v |
Backspace, Form Feed, Vertical Tab – mentioned for completeness, coming from old times, not used nowadays (you can forget them right now). |
Arrays
Array is collection of similar data types
Array methods
arr.push(...items) – adds items to the end,arr.pop() – extracts an item from the end,arr.shift() – extracts an item from the beginning,arr.unshift(...items) – adds items to the beginning.The two most used data structures in JavaScript are Object and Array.
let arr = ["John", "Smith"]
let [firstName, surname] = arr;
alert(firstName);
Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables, as sometimes that’s more convenient.
let options = {
title: "Menu",
width: 100,
height: 200
};
let {title, width, height} = options;
OBJECT-DESTRUCTURING
String methods
String length
String slice()
String substring()
String substr()
String replace()
String replaceAll()
String toUpperCase()
String toLowerCase()
String concat()
String trim()
String charAt()
String charCodeAt()
String split()
String startsWith()
String EndsWith()
Array methods
Adding and removing elements from an array
Array methods
Finding the elements in the array
High-order array methods
High-order array methods are accepts the call back function.
Array methods
Some other array methods
Scheduling: setTimeout and setInterval
We may decide to execute a function not right now, but at a certain time later. That’s called “scheduling a call”.
There are two methods for it:
setTimeout allows us to run a function once after the interval of time.setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.Many functions are provided by JavaScript host environments that allow you to schedule asynchronous actions. In other words, actions that we initiate now, but they finish later.
For instance, one such function is the setTimeout function.
new Date() Object in js
getFullYear()
getMonth()
getDate()
getDay()
getHours()
getMinutes()
getTime()
getMilliseconds()
OOPS IN JS
Use the keyword class to create a class.
Always add a method named constructor():
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
}
IMPORTS AND EXPORTS IN JS
JavaScript modules allow you to break up your code into separate files.
This makes it easier to maintain a code-base.
Modules are imported from external files with the import statement.
Modules also rely on type="module" in the <script> tag.
Ex: import { name, age } from "./person.js";
Intro DOM (DOCUMENT OBJECT MODEL)