Check if property exists in Javascript

Quickly check whether a property exists against an object. There is more than one way to check whether a given object has a prop - the choice if often convenience rather than performance of the said method. Let’s see the various methods by taking this example of a simple object. let apple = { color: "red", size: "small" }; Method 1: Check the value of the prop Check whether a property exists in an object by “getting” that prop. ...

Find and Replace Substring in Javascript

Find and replace substring within a string using replace. If you know the exact substring use it directly in replace and be at peace. let sample = "Lorem ipsum dolor sit amet ..."; sample = sample.replace("ipsum", "gypsum"); console.log("sample: ", sample); // Lorem gypsum dolor sit amet ... Or, use regular expressions and the massive advantages that come with it. sample = sample.replace(/Lorem/gi, "loremi"); console.log("sample: ", sample); // loremi ipsum dolor sit amet ... If you have special characters that cannot be handled by regular expressions. ...

Right Way to Use Console for Devtools

Here are some effective ways of inspecting variables used in your code using browser dev tools to your advantage. This is what we typically do in the code to know what exactly is happening with our variables. var name = "Mr. Anderson"; console.log('before loop" + name); This will have the following beautiful output in browser console. Let’s dig deeper. Always use commas Instead of a plain string pass variables separated by comma in console. This will present a navigable tree instead of an undecipherable object. ...

Form Data Validation in Client and Server

I am using Vue quite a bit and Vuelidate/ VeeValidate have spoilt me with options. The client-side validations are quite neat and easy to do. All you need is a couple of lines of code, some rules in the script section and you are good to go. However, you should not stop at this. Validation needs to occur in the server in addition to the client for all important data. ...

Filter Duplicate Attributes in Array of Objects in Javascript

Catch duplicate attributes within an array of objects and retrieve unique elements. Array/object in question - const students = [ { name: "Rama", grade: 10 }, { name: "Kris", grade: 5 }, { name: "Pete", grade: 7 }, { name: "Mo", grade: 5 } ]; We want to get unique grades from the array. Option 1: Quickest way - use set Use a set to get unique values. We had a quick post on sets in Javascript just yesterday. const uniqueGradesSet = [...new Set(students.map(stu => stu["grade"]))]; console.log("uniqueGradesSet: ", uniqueGradesSet); // [ 10, 5, 7 ] Option 2: Use object + array combination Use an object with each iteration to check and filter duplicates. ...

Use Set in Javascript

Set was introduced in ES6 and is a collection of values without keys. It has one killer feature that you want to use - filtering duplicates owing to its feature of allowing only unique values. So.. set? People from other languages may be familiar with sets, maps and arrays.. and the confusion that comes with them. What is a set (map, or array)? Why should I use it? When should I use what? How is it different? Why am I alive? What is the purpose of life? As a Javascript developer, I was immune to that - use arrays for series of elements, objects for rest (aka keyed elements). ...

ES6 String Operations That Rock the World

There are many great improvements provided by ES6 (introduced in Y2015), but I think a couple of them are really cool. String includes includes checks whether a certain (sub) string exists in the provided string. Note that this operation is case sensitive. let str1 = "Neo rocks the world"; console.log("rocks? ", str1.includes("rocks")); // rocks? true console.log("is agent? ", str1.includes("agent")); // is agent? false Not quite complicated, but makes the language that much more friendly :) Earlier, we used a very complicated piece of code to achieve the same result. ...

Private Variables in Javascript Class

We define a class and its variables like so - class UserData { name = ""; type = ""; internalUserFlag = false; constructor({ name, type }) { this.name = name; this.type = type; } } const student1 = new UserData({ name: "Neo" }); console.log("student1: ", student1); // UserData { name: '', type: '', internalUserFlag: false } See quickly get started with Javascript classes if you want to know more. We can potentially extend the class and have multiple validation rules. We also like to keep variables like type and internalUserFlag private. These variables can be changed by extension classes, but not by external methods. ...

Extending Class in Javascript

Extend a class and create a sub class for easier maintainability. Sub class can make use of special super functions to extend the functionality of a class. Using sub classes you can build out a complex logic in steps rather than as one messy module. See how to start with a Javascript class and class prototypes if you are new to Javascript classes. Consider the example from our prototype discussion- User.prototype.users = []; class UserData { user = { name: "", type: "", }; setUser({ name, type }) { this.user.name = name; this.user.type = type; } addUser() { this.users.push(this.user); } } UserData.prototype.users = []; user1 = new UserData(); user1.setUser({ name: "Rama", type: "Teacher" }); user1.addUser(); console.log(user1.users); // [ { name: 'Rama', type: 'Teacher' } ] user2 = new UserData(); user2.setUser({ name: "Kris", type: "Student" }); user2.addUser(); console.log(user2.users); /* output [ { name: 'Rama', type: 'Teacher' }, { name: 'Kris', type: 'Student' } ] */ console.log(user1.users); // // [ { name: 'Rama', type: 'Teacher' } ] /* output [ { name: 'Rama', type: 'Teacher' }, { name: 'Kris', type: 'Student' } ] */ Our application works beautifully and allows us to define users. But, what if we want to store information specific to teachers and students? ...

Using Prototypes in Javascript Class

Prototypes enable us to define attributes and functions of a class that can be shared across its object instances. Let us look at how we could start using prototypes in our Javascript classes. See how to start with a Javascript class if you are new to Javascript classes. Class in Javascript is built internally using prototypes - in fact that was the way some of us were reaching OOP nirvana in Javascript back in the day. ESXX standards have changed all that and now we have classes and all the goodness therein. ...