Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const x = 4;
- if(x === 10){ //=== will match the data type, as well.
- 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 x1 = 4;
- const y1 = 11;
- if(x1 > 5 || y1 > 10){ //or statement
- console.log('x is more than 5 or y is more than 10');
- }
- const x2 = 6;
- const y2 = 11;
- if(x2 > 5 && y2 > 10){ //and statement
- console.log('x is more than 5 or y is more than 10');
- }
- const z = 11; //Turnurary operator (?? not sure about these yet)
- const color = z > 10 ? 'red' : 'blue';
- console.log(color);
- 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');
- }
- //Basic Functions
- function addNums(num1 = 1, num2 = 1){ //default values can be overridden
- return num1 + num2;
- }
- console.log(addNums(5,6)); //passing numbers through this function
- // Arrow functions
- const addNums2 = (num1 = 1, num2 = 1) => num1 + num2; //default values can be overridden
- console.log(addNums2(5,5));
- // Arrow functions
- const addNums3 = num1 => num1 + 5; //function addNum3 = a number and returns that number + 5
- console.log(addNums3(3));
- //Object Oriented Programming
- //Constructor Functions
- /*
- function Person(firstName, lastName, dob){
- this.firstName = firstName;
- this.lastName = lastName;
- this.dob = new Date(dob); //can put in different date forms.
- // this.getBirthYear = function(){
- // return this.dob.getFullYear();
- //}
- //this.getFullName = function(){
- // return `${this.firstName} ${this.lastName}`;
- //}
- }
- Person.prototype.getBirthYear = function(){
- return this.dob.getFullYear();
- }
- Person.prototype.getFullName = function(){
- return `${this.firstName} ${this.lastName}`;
- }
- */
- //Class. Does the same as above, just prettier.
- class Person {
- constructor(firstName, lastName, dob){
- this.firstName = firstName;
- this.lastName = lastName;
- this.dob = new Date(dob);
- }
- getBirthYear(){
- return this.dob.getFullYear();
- }
- getFullName(){
- return `${this.firstName} ${this.lastName}`;
- }
- }
- //instantiate object with constructor function
- const person1 = new Person('John', 'Doe', '19 March 2019');
- const person2 = new Person('James', 'Dobbs', '4-25-2000');
- // console.log(person1);
- // console.log(person2.dob); // gets only dob
- //console.log(person1.getBirthYear());
- //console.log(person2.getFullName());
- console.log(person2.getFullName());
- console.log(person1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement