Inside the computer’s world, there is only data. You can read data, modify data, create new data but anything that isn’t data simple does not exist.
\n
(+)
(+)
(-)
(*)
(/)
(%)
(>)
(<)
(>=)
(<=)
(==)
(!=)
(===)
(&&)
(||)
(!)
( ?: )
Automatic Typc Converstion using Type Coercion
Expressions and Statements
Variables
Functions
if (expression1)
statement1;
else if (expression2)
statement2;
else
statement3;
Example
let num = Number(prompt("Enter a number"));
if (num <= 10) {
alert("Too Low");
} else if (num > 10 && num <= 50) {
alert("Bingo !! Correct Answer");
} else {
alert("Way Too High");
}
while (expression) {
statement
}
Example
let i=1;
while (i<=10) {
console.log(i);
i++;
}
for (let i=0; i<=10; i++ ){
statement
}
Example
let result;
for (let i=0; i<=10; i++) {
result += i;
}
console.log(result);
break
keyword can be used to break out of loopcontinue
keyword can be used to continue with next loop’s next executionlet choice = prompt("Enter option [A], [B], [C], [D]");
switch (choice) {
case 'A':
console.log("A Selected");
break;
case 'B':
console.log("B Selected");
break;
case 'C':
console.log("C Selected");
break;
case 'D':
console.log("D Selected");
break;
default:
console.log("Unknown Option Selected");
break;
}
// Sinle Line Comment
/*
Example of
Multiline Comment
*/
Functions are the Bread and Butter of JavaScript
function
return
statement determines the value the function returnslet fn_sayHello = function(name) {
return "Hello " + name;
}
alert(fn_sayHello("User"));
Declaring fn_sayHello to be a function
function fn_sayHello(name) {
return "Hello " + name;
}
A function that “closes over” some local letiable is called a Closure.
function multiplier(factor) {
return function(number) {
return factor * number;
}
}
let twice = multiplier(2);
console.log(twice(5));
>>10
//Array.forEach
function logger (element, index) {
console.log("Index: " + index + ": " + element);
}
let countries = ["India", "Nepal", "China"]
countries.forEach(logger);
//Array.filter
function filterAge(element) {
return element.age >= 21;
}
let people = [
{name: "Aayush",age: 28},
{name: "Aadesh",age: 18},
{name: "Bob",age: 52}
]
let peopleLegal = people.filter(filterAge);
console.log(peopleLegal);
let obj = {
property1: "Value1",
property2: "Value2",
method1: function(args){
console.log(args);
}
}
let obj = {name: "A", date: new Date(), scores: [1, 2, 3]};
for (let p in obj) {
if (obj.hasOwnProperty(p)) {
console.log(p + ': ' + object[p]);
}
}
let str_message = "Welcome to JavaScript";
let str_Hello = 'I am String to ';
console.log( str_message.slice(0,5) );
console.log( str_message.indexOf("to") );
console.log( str_Hello.indexOf("to") );
console.log( str_Hello.trim() );