Posts

Showing posts from February, 2025

JavaScript: Loops

  Loops I. What is a loop? A loop repeats a set of instructions until a specified condition, called a stopping condition, is reached. II. For loop A for loop contains three expressions separated by a semi-colon inside the parentheses: an initialization starts the loop. a stopping condition. If the condition evaluates to true, the code block will run, and if it evaluates to false the code will stop. an iteration statement updates the iterator variable on each loop. For example for (let i = 0 ; i < 3 ; i++) { console.log(i); } // Output: 0 ; 1 ; 2 ; In the code block above: The initialization is  let i = 0 , so the loop will start counting at 0. The stopping condition is  i < 3 , so the loop will run as long as i is less than 4. The iteration statement is  i++ . After each loop, the count value will increase by 1. For the first iteration, the counter will equal 0. For the second iteration, the counter will equal 1, and so on. The code block...

JavaScript: Object Prototypes

  Object Prototypes What are object prototypes? How to use them? Javascript Prototypes All objects in Javascript have a prototype, which inherits properties and methods from their prototype. We can move the functions in the object instance and put them in a prototype. // Constructor function Person ( firstName, lastName, birthDay ) { this .firstName = firstName; this .lastName = lastName; this .birthDay = new Date (birthDay); } //getFullName Person.prototype.getFullName = function ( ) { return ` ${ this .firstName} ${ this .lastName} ` ; }; // getBYear Person.prototype.getBYear = function ( ) { return this .birthDay.getFullYear(); }; // Instatiate an object const p1 = new Person( "Anna" , "Wood" , "3-25-1995" , "green" ); const p2 = new Person( "Joe" , "Hopkins" , "10-12-1990" , "blue" ); console .log(p2.getFullName()); // Joe Hopkins

JavaScript: Constructor Function in Objects

  Constructor Function In this article, we will learn the constructor function and its usage. I. What is the constructor function? A constructor function is a function to create an object. II. How to create a constructor function? When we create a constructor function, we should capitalize the function’s name. Then, we pass in the properties we want to set in the parameters. We use the keyword  this  to set them as the object’s properties. Avoid using  arrow functions  when using  this  in a method. function Person ( name, birthDay, color ) { this .name = name; this .birthDay = new Date (birthDay); this .color = color; } III. Create an object instance Once we have an object constructor, we use the  new  keyword to create new objects of the same type. const p 1 = new Person( "Anna" , "3-25-1995" , "green" ); const p 2 = new Person( "Joe" , "10-12-1990" , "blue" ); console .log(p 1 ); // Person {name: "Anna...

JavaScript: Objects

  JavaScript Objects I. What is a Javascript Object? JavaScript objects are containers for named values. An object has a list of values written as  key:value  pairs, with the keys and the values separated by colons. We use curly braces {} to create an object: const car = { color : 'blue' ; brand: 'Honda' ; }; Note : Spaces and line breaks are not important. An object definition can span multiple lines. II. Access properties in an object You can access, add or edit a property within an object using dot notation or bracket notation. We must use bracket notation when accessing keys with numbers, spaces, or special characters. objectName.propertyName; // or objectName[ "propertyName" ]; Example const person = { name : "Anna" , age : 22 , hobbies : [ "movies" , "travel" , "art" ], address : { street : "12 Anderson" , city : "Copenhagen" , country : "Denmark" , },...