Posts for Javascript

How to Get Current Date in Javascript

In this article, you will learn how to get the current date in Javascript. Let’s say you have an empty variable named ‘a’. In order to get the current date, you can use the new keyword and Date object. Note: The new keyword functions by creating an instance of an object. The Date object represents […]

February 17, 2021 in Code examples

How to Truncate A String in Javascript

In this article, you will learn how to truncate a string in Javascript. Let’s say you have a string variable named ‘a’ with value “I love code-source.tempurl.host!”. In order to truncate a string, you can use the substring() method. In this example, you will be extracting the first 6 characters from ‘a’. Note: The substring() […]

February 17, 2021 in Code examples

How to Reverse A String in Javascript

In this article, you will learn how to reverse a string in Javascript. Let’s say you have a string variable named ‘a’ with value “codesource”. In order to reverse the string variable, you can use the split(), reverse() and join() methods. Note: The split() method functions by splitting a string into an array of substrings. […]

February 17, 2021 in Code examples

How to Compare Dates in Javascript

In this article, you will learn how to Compare Dates in Javascript. Let’s say you have two dates: 14 / 2 / 2020 and 14 / 2 / 2021. In order to compare dates, you can use the getTime() method and a strict equality comparison ===. Note: The getTime method functions by returning the number […]

February 14, 2021 in Code examples

How to Capitalize Strings in JavaScript

In this article, you will learn How to Capitalize Strings in JavaScript. Let’s say you have a string variable named ‘a’ with the value “codesource”. In order to capitalise the value of a, you can use the toUpperCase() method. Note: The toUpperCase() method functions by converting all alphabetical characters in a string to uppercase letters.

February 14, 2021 in Code examples
Deven
Deven wrote

Javascript filter array multiple values – example

In JavaScript to filter multiple values in an Array, you can use filter() array function. JavaScript filter multiple values in an Array consider the example below: In the above code, we can filter an array of objects by testing whether the properties match a certain set of criteria. We can also use template object for […]

February 11, 2021 in Code examples
Deven
Deven wrote

Fix – Javascript try if document.body.innerhtml var a not working

If Javascript try if document.body.innerhtml is not working for you. The main reason for this error can be getting Aside from overwriting page HTML with a single value each iteration. When you replace the entire page HTML and break its javascript events such as click/hover handlers and many others attached via addEventListener or .on properties. Also, […]

February 11, 2021 in Errors solved
Deven
Deven wrote

Create a Custom Class in Javascript Using the Constructor Pattern

In this article, you will learn to create a Custom Class in Javascript Using the Constructor Pattern. Constructor Pattern can also help you understand how JavaScript classes really work. Consider the following example to create a Custom Class in Javascript Using the Constructor Pattern. In the code snippet above first, you write a constructor function […]

February 10, 2021 in Code examples