Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Javascrit

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

/********************Linking a Javascript file******************/

// let js = "amazing";
// console.log(40 + 8 + 23 - 10);

/********************* Values and Variables***************** */


// console.log("Jonas");
// console.log(23);

// let firstName = "Matilda";

// console.log(firstName);
// console.log(firstName);
// console.log(firstName);

/*******************Variable name conventions****************/


// let jonas_matilda = "JM";
// let $function = 27;

// let person = "jonas";


// let PI = 3.1415;

// let myFirstJob = "Coder";


// let myCurrentJob = "Teacher";

// let job1 = "programmer";


// let job2 = "teacher";

// console.log(myFirstJob);

/************************Data Types***************/
// let javascriptIsFun = true;
// console.log(javascriptIsFun);

// // console.log(typeof true);
// console.log(typeof javascriptIsFun);
// // console.log(typeof 23);
// // console.log(typeof 'Jonas');

// javascriptIsFun = 'YES!';
// console.log(typeof javascriptIsFun);

// let year;
// console.log(year);
// console.log(typeof year);

// year = 1991;
// console.log(typeof year);

// console.log(typeof null);

/*****************let, const and var****************/


// let age = 30;
// age = 31;

// const birthYear = 1991;


// // birthYear = 1990;
// // const job;

// var job = 'programmer';


// job = 'teacher'

// lastName = 'Schmedtmann';
// console.log(lastName);

/********************* Basic Operators ********************/


// ****************** Math operators
// const now = 2037;
// const ageJonas = now - 1991;
// const ageSarah = now - 2018;
// console.log(ageJonas, ageSarah);

// console.log(ageJonas * 2, ageJonas / 10, 2 ** 3);


// // 2 ** 3 means 2 to the power of 3 = 2 * 2 * 2

// const firstName = 'Jonas';


// const lastName = 'Schmedtmann';
// console.log(firstName + ' ' + lastName);

// ******************************Assignment operators
// let x = 10 + 5; // 15
// x += 10; // x = x + 10 = 25
// x *= 4; // x = x * 4 = 100
// x++; // x = x + 1
// x--;
// x--;
// console.log(x);

// *********************************Comparison operator
// console.log(ageJonas > ageSarah); // >, <, >=, <=
// console.log(ageSarah >= 18);

// const isFullAge = ageSarah >= 18;

// console.log(now - 1991 > now - 2018);

// *********************************Operator Precedence
// const now = 2037;
// const ageJonas = now - 1991;
// const ageSarah = now - 2018;

// console.log(now - 1991 > now - 2018);

// let x, y;
// x = y = 25 - 10 - 5; // x = y = 10, x = 10
// console.log(x, y);

// const averageAge = (ageJonas + ageSarah) / 2;


// console.log(ageJonas, ageSarah, averageAge);

/********Strings and Template Literals**********/


// const firstName = 'Jonas';
// const job = 'teacher';
// const birthYear = 1991;
// const year = 2037;

// const jonas = "I'm " + firstName + ', a ' + (year - birthYear) + ' year old ' +
job + '!';
// console.log(jonas);
// const jonasNew = `I'm ${firstName}, a ${year - birthYear} year old ${job}!`;
// console.log(jonasNew);

// console.log(`Just a regular string...`);

// console.log('String with \n\


// multiple \n\
// lines');

// console.log(`String
// multiple
// lines`);

/***********if else statement:- *****************/

// let js = "amazing";
// if (js == "amazing") {
// } else {
// alert("Learning js is Boring!");
// }

/*****************alert function*****************/
// alert("Learning js is really Fun!");
// console.log(5 + 6 + 45);
// let firstname = "Aryan gupta";
// console.log(firstname);

/******************data types**************/
// let age = 23; // number data types
// let Firstname = "Aryan"; // string data types
// let fullAge = true; // Boolen data types
// let children; // undefined as no value is assigned to it
// let n = null; // empty vale;
// symbol is also a data type which we discussed later
// BigInt is anather data types which we also disscuss later in course
// console.log(2.6 + 7.4);
// console.log(typeof 2.6);
// firstname = "dhairya";
// console.log(firstname);
// console.log(2 ** 3);
// const firstname = "Aryan";
// const position = "student";
// const birthyear = 2003;
// const year = 2022;
// const aryan =
// "I'm " +
// firstname +
// " ,a " +
// (year - birthyear) +
// " years old " +
// position +
// "!";
// console.log(aryan);

/*****************template literal*********************/
// const aryannew = `I'm ${firstname}, a ${
// year - birthyear
// } year old ${position}!`;
// console.log(aryannew);

/*****************strings with new line********************/


// console.log(`strings with \nnew line`);
// console.log(`by entering enter
// multi line string
// is created`);

/********************if else***************************/
// const age = 18;
// const isoldenough = age >= 18;
// if (isoldenough) {
// console.log(`aryan can start driving`);
// } else {
// console.log(`you need to wait for ${18 - age} more years to start driving!`);
// }

/*****************typeconversion*********************/
// const inputYear = "1991";
// console.log(Number(inputYear), inputYear);
// console.log(Number(inputYear) + 18);

// console.log(Number("aryan"));
// console.log(typeof NaN);
// console.log(String(28));

/***********type coercion*******************/
// need to be updated later
*****************************imp**************************//
/***************ternary operator******************/
//need to be updated later
*****************************imp**************************//
/************ Getting an input from a user***********/
// const query = prompt(`what is your favourite number: `);
// console.log(query);

/*************strict mode***************/
//activating strict mode below:-
"use strict"; // need to be first line of js file otherwise not work comment
ignored
// strict mode enabled 🙂🙂
// strict mode help us to find the errors more precisely and keep the list of
reserved words which can be used in future
// for example:-
// let drivings = false;
// const testPass = false;
// let testPass = prompt("test passed?"); ignore this message for now
// if (testPass) drivings = true;
// if (drivings) console.log("You can drive now🙂👍");
// else console.log(`you can't drive until 7 month😱😨`);

/***************FUNCTION**************/
// -> A function holds one or more complete lines of codes .

// function logger() {
// console.log("welcome to our five star to our restaurant✨🌟");
// console.log(`your order for ${fruitProcessor(9, 5)} is completed`);
// let is = prompt("enter yes if you want to visit again!❓");
// if (is === "yes") {
// logger();
// } else {
// console.log("Hope you have enjoyed. Please visit again🙂");
// }
// }

// function fruitProcessor(apples, oranges) {


// console.log(apples, oranges);
// const juice = `juice with ${apples} apples and ${oranges} oranges.`;
// return juice;
// }
// -> calling || running || invoking a function
// logger();

/************ Function Declaration**************/


// function calcage1(birthYear, currentYear) {
// return currentYear - birthYear;
// }
// const age1 = calcage1(2003, 2022);
// console.log(age1);
// !!NOTE: you can call a function declaration before you define it

/*************** Function Expression *******************/


// const calcage2 = function (birthYear, currentYear) {
// // calcage2 is now a function
// return currentYear - birthYear;
// };
// // -> calling || running || invoking a function
// console.log("age : ", calcage2(2003, 2022));
// !!NOTE: In JAVA SCRIPT functions are actually just values
// !!NOTE: WE CAN'T call a function expression before defining it .

/************ Arrow Function *************/


// const calcage3 = (birthYear) => 2022 - birthYear; // a form of function
expression // this helpfull for one liner function
// const age3 = calcage3(2003);
// console.log("age : ", age3);

// const calcage4 = (birthYear, currentYear) => currentYear - birthYear; // a form


of function expression // this helpfull for one liner function
// const age4 = calcage4(2003, 2022);
// console.log("age : ", age4);

// -> a function for calculating years until retirement(complex part)


// const YearsUntilRetirement = (age6 = currentYear - birthYear, retirementYear,
firstname) => {
// return `${firstname} Retires in ${retirementYear - age6} years!`;
// };
// const age9 = YearsUntilRetirement(2022 - 2003, 60, 'ARYAN');
// console.log(age9);

/************ ARRAYS ***************/


// -> an array is like a big container into ehich we can throw variables and then
later reference them
// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// console.log(friends);
// !!NOTE: Arrays can hold as many values as we want and also value of any type
we'd like.

// const years = new Array(1976, 1975, 2002, 2003, 2006, 2022)


// console.log(friends[0]);
// console.log(friends[1]);
// console.log(friends[2]);
// console.log(friends[3]);
// console.log(friends[4]);
// console.log(friends[5]);
// console.log(friends.length);
// console.log(friends[friends.length - 1]);

// changing the elements of an array


// friends[2] = 'Aryan';
// console.log(friends);

// !!NOTE: primitive value are immutable i.e, can't be changed if declared with
'const' but array is not primitive so we can change the elements of an array but
not the whole array

// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];


// const firstname = 'Aryan';
// const lastname = 'Gupta';
// const profile = [firstname, lastname, 2022 - 2003, friends];
// console.log(profile);

// exercise#1
// const calcage = (birthYear, currentYear) => currentYear - birthYear;
// const years = [1990, 1967, 2002, 2003, 2005];

// console.log('age1: ', calcage(years[0], 2022));


// console.log('age2: ', calcage(years[1], 2022));
// console.log('age3: ', calcage(years[2], 2022));
// console.log('age4: ', calcage(years[3], 2022));
// console.log('age5: ', calcage(years[4], 2022));

// const ages = [calcage(years[0], 2022), calcage(years[1], 2022),


calcage(years[2], 2022), calcage(years[3], 2022), calcage(years[4], 2022)];
// console.log(ages);
// ***********xxxxxxxxxxxxxxxxxxxxxxxxxxxx************

/**************ARRAYS Methods*******************/
// 1) push : push method adds element to the end of an Array.
// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// friends.push(`himanshu ${18} years old`);
// console.log(friends);

// 2) unshift : adds an element to the beginning of an Array.


// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// friends.unshift(`himanshu ${18} years old`);
// console.log(friends);

// 3) pop : pop will remove the last element from the Array and is just opposite of
push method.
// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// friends.pop();
// console.log(friends);
// const removedElement = friends.pop() // this returns the remove element.
// console.log(removedElement);

// 4) shift: removes the first elements from the array .


// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// friends.shift();
// const removedElement = friends.shift();
// console.log(removedElement);
// console.log(friends);

// 5) indexof : it tells us in which position certain elements is in the array.


// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// console.log(friends.indexOf('lalit'));
// console.log(friends.indexOf('rohit')); // will return -1 as it is not present
in array

// 6) includes : includes instead of returning the index of the element will simply
return true if the element is in the array and false if it's not .
// const friends = ['sohail', 'manas', 'lalit', 'dhairya', 'ayush'];
// console.log(friends.includes('Aryan'));
// console.log(friends.includes('manas'));

// !!NOTE: includes method uses strict equality for this check. example: if we push
some value to this array and is number instead of string so friends.push() which
remember will add a value to the end of the array . so if we check for that added
element it will not work , so it would say false,and this is because testing with
strict equality which means it will not do type coercion. and if both matches the
type it will work!;

// !!NOTE: we can use includes method to write conditionals.

// ********add more methods to this after studying


it........................................

/****************Introduction to OBJECTS ******************/


// -> IN ARRAYS we can't reference variables by name but only by their order
number.
// --> OBJECTS: we actually define key values pairs , so then we can give each of
these values here, a name .

// const calcage = (birthYear, currentYear) => currentYear - birthYear;

// const profile = {
// firstName: `Aryan`,
// lastName: 'Gupta',
// age: calcage(2003, 2022),
// profession: 'student',
// job: 'coder',
// friends: ['sohail', 'manas', 'lalit', 'dhairya', 'ayush', 'vivek', 'Himanshu
sahu']
// };

// !!NOTE: In object order of elements doesn't matters while in arrays it matters.

// -> accesing elements of objects by dot operator


// console.log(profile.firstName); // accesing elements by dot operator
// console.log(profile.lastName); // accesing elements by dot operator
// console.log(profile.friends); // accesing elements by dot operator
// console.log(profile.age); // accesing elements by dot operator
// console.log(profile.profession); // accesing elements by dot operator
// console.log(profile.job); // accesing elements by dot operator

// -> accesing elements of objects by brackets.


// const nameKey = 'Name';
// console.log(profile['first' + nameKey]);
// console.log(profile['last' + nameKey]);

// const interestedIn = prompt('what do you want to know about Aryan? choose


between firstname, lastname, age, profession, job and friends!');
// if (profile[interestedIn]) {
// console.log(profile[interestedIn]);
// } else {
// console.log('you have entered a Incorrect value');
// }

// -> adding an element to the objects.


// 1) by dot operator.
// profile.location = 'INDIA';
// 2) by bracket notation.
// profile['hobbies'] = '@Coder';
// console.log(profile);

// --> challenge
// " jonas has 3 friends and his best friend is called 'michael'"

// solution
// console.log(`${profile.firstName} has ${profile.friends.length} friends and his
best friend is '${profile.friends[profile.friends.indexOf('dhairya')]}'.`);

/***************** OBJECT Methods***************/

// const profile = {
// firstName: `Aryan`,
// lastName: 'Gupta',
// age: 19,
// profession: 'student',
// job: 'coder',
// friends: ['sohail', 'manas', 'lalit', 'dhairya', 'ayush', 'vivek', 'Himanshu
sahu'],
// birthyear: 2003,
// currentYear: 2022,
// hasDriverLicence: true,
// // calcAge: () => this.currentYear - this.birthyear // shows error
// calcAge: function () {
// console.log(this);
// return this.currentYear - this.birthyear;
// }
// };
// console.log(profile.calcAge());

You might also like