Javascript
//single line comment
/* multi
line
comment*/
// let, const
//string, numbers, boolean, null, undefined
const name = 'hemanth';
const age = 28;
const rating = 4.5;
const isCool = true;
const x = null;
const y = undefined;
let z;
console.log(typeof name);
//strings
const name = 'hemanth';
const age = 28;
//concatination
console.log('my name is '+name+ ' and I am '+ age);
//template string
console.log(`my name is ${name} and I am ${age}`);
const hello = `my name is ${name} and I am ${age}`;
console.log(hello);
//string functions
const s ='hello world';
console.log(s.length);
console.log(s.toUpperCase());
console.log(s.toLowerCase());
console.log(s.substring(0, 5));
console.log(s.substring(0,5).toUpperCase());
console.log(s.split(''));
const t = 'technology, computers, it, code';
console.log(t.split(', '));
//arrays
/* array using constructor, new keyword and
something after that means it is called constructor */
const numbers = new Array(1,2,3,4,5);
console.log(numbers);
//we can give multiple types in single array in js
const fruits = ['apples', 'oranges', 'pears', 10, true];
const fruits = ['apples', 'oranges', 'pears'];
console.log(fruits);
console.log(fruits[1]);
fruits[3] = 'grapes';
console.log(fruits);
fruits.push('mangos'); //add item to last
fruits.unshift('strawberries');//add item to front
fruits.pop();//last one off
console.log(Array.isArray(fruits));
console.log(fruits.indexOf('oranges'));
//object literals
const person = {
firstName: 'hemanth',
lastName: 'kumar',
age: 28,
hobbies: ['music', 'movies', 'sports'],
address: {
street: '50 main st',
city: 'boston',
state: 'ma'
}
}
console.log(person);
console.log(person.firstName);
console.log(person.firstName, person.lastName);
console.log(person.hobbies[1]);
console.log(person.address.city);
//structuring
const {firstName, lastName, address:{city}} = person;
console.log(firstName);
console.log(city);
person.email='hemanth@mail.com';
console.log(person);
//array of objects
const todos = [
{
id:1,
text:'take out trash',
isCompleted:true
},
{
id:2,
text:'meeting with boss',
isCompleted:true
},
{
id:3,
text:'dentist appt',
isCompleted:false
},
];
console.log(todos);
console.log(todos[1].text)
const todoJSON = JSON.stringify(todos);
console.log(todoJSON);
//for
for(let i=0;i<=10;i++) {
console.log(i);
}
for(let i=0;i<=10;i++) {
console.log(`for loop number: ${i}`);
}
//while
let i=0;
while(i<10) {
console.log(i);
i++;
}
//do while
let i = 1;
do {
console.log(i);
i++;
} while (i <= 10);
//break
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break; // exits the loop
}
console.log(i);
}
//continue
for (let i = 1; i <= 10; i++) {
if (i === 5) {
continue; // skip when i is 5
}
console.log(i);
}
const todos = [
{
id:1,
text:'take out trash',
isCompleted:true
},
{
id:2,
text:'meeting with boss',
isCompleted:true
},
{
id:3,
text:'dentist appt',
isCompleted:false
},
];
for (let i=0;i<todos.length;i++) {
console.log(todos[i].text);
}
for (let todo of todos) {
console.log (todo.text);
}
//for of
const numbers = [10, 20, 30];
for (const num of numbers) {
console.log(num);
}
//foreach, map, filter
todos.forEach(function(todo) {
console.log(todo.text);
});
const todoText = todos.map(function(todo) {
return todo.text;
});
console.log(todoText);
const todoCompleted = todos.filter(function(todo) {
return todo.isCompleted === true;
});
console.log(todoCompleted);
const todoCompleted = todos.filter(function(todo) {
return todo.isCompleted === true;
}).map(function(todo) {
return todo.text;
})
console.log(todoCompleted);
//foreach with syntax
array.forEach(function(currentValue, index, array) {
// code to execute for each element
});
let numbers = [10, 20, 30, 40];
numbers.forEach(function(num) {
console.log(num);
});
//map with syntax
let newArray = oldArray.map(function(element, index, array) {
// return something for newArray
});
let numbers = [1, 2, 3, 4, 5];
// Using map to double each number
let doubled = numbers.map(function(num) {
return num * 2;
});
console.log("Original array:", numbers);
console.log("Doubled array:", doubled);
//filter with syntax
let newArray = oldArray.filter(function(element, index, array) {
// return true to keep element, false to skip it
});
let numbers = [1, 2, 3, 4, 5, 6];
// Using filter to select even numbers
let evenNumbers = numbers.filter(function(num) {
return num % 2 === 0;
});
console.log("Original array:", numbers);
console.log("Even numbers:", evenNumbers);
1. forEach():- Runs a function for every element in an array.
const numbers = [1, 2, 3, 4];
numbers.forEach(function(num) {
console.log(num * 2);
});
Output:-
2
4
6
8
2. map():- Creates a new array by transforming each element.
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(function(num) {
return num * 2;
});
console.log(doubled);
Output:-
[2, 4, 6, 8]
3. filter():- Creates a new array with elements that pass a condition.
const numbers = [1, 2, 3, 4];
const evenNumbers = numbers.filter(function(num) {
return num % 2 === 0;
});
console.log(evenNumbers);
Output:-
[2, 4]
const x = 10;
if(x===10) {
console.log('x is 10');
} else if(x>10) {
console.log('x is greater than 10');
}else {
console.log('x is less than 10');
}
const x = 10;
const y = 11;
if(x>5||y>10) {
console.log('x is more than 5 or y is more than 10');
}
const x = 11;
const color = x>10?'red': 'blue';
console.log(color);
const color = 'green';
switch (color) {
case 'red':
console.log('color is red');
break;
case 'blue':
console.log('color is blue');
break;
default:
console.log('color is not red or blue');
break;
}
//function
function addNums(num1 =1, num2 = 1) {
return num1 + num2;
}
console.log(addNums(5,5));
//arrow function
const addNums=(num1 =1, num2 = 1)=> {
return num1 + num2;
}
console.log(addNums(5,5));
const addNums=(num1 =1, num2=1)=>{
console.log(num1+num2);
}
addNums(5, 5);
const addNums=(num1 =1, num2=1)=> console.log(num1+num2);
addNums(5, 5);
const addNums=(num1 =1, num2=1)=> num1+num2;
console.log(addNums(5, 5));
const addNums=num1=>num1+5;
console.log(addNums(5));
//constructor ex 1
function Person() {
this.name="unknown";
this.age=0;
}
const person1=new Person();
console.log(person1.name);
console.log(person1.age);
//constructor ex 2
function Person(name, age) {
this.name = name;
this.age = age;
}
const person1= new Person("hemanth", 28);
const person2= new Person("unknown", 30);
console.log(person1.name, person1.age);
console.log(person2.name, person2.age);
//constructor ex 3 adding methods inside constructor
function Person(name, age) {
this.name = name;
this.age = age;
this.greet=function() {
return `hello I am ${this.name}`;
};
}
const person= new Person("hemanth", 28);
console.log(person.greet());
//constructor ex 4 using prototype
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.greet=function() {
return `hello I am ${this.name}`;
};
const person1= new Person("hemanth", 28);
console.log(person1.greet());
Comments
Post a Comment