Found 6710 Articles for Javascript

Object literals vs constructors in JavaScript

Aman Kumar
Updated on 19-Dec-2022 12:23:29

1K+ Views

Object literals and constructors both are used to create an Object in JavaScript. An object created by the object literals is a singleton. This means when a change is made to the object, it affects that object across the entire script. If an object created by a function constructor has multiple instances of the object. This means the changes made to one instance, will not affect other instances. Object Literal Notation − Let’s create user details where we have the key, name, age, and name of the company. So we are creating an object named userDetails. const userDetails = {name: "Aman", ... Read More

Difference between res.send and res.json in Express.js

Ayush Gupta
Updated on 02-Dec-2019 07:00:51

2K+ Views

Whenever an Express application server receives an HTTP request, it will provide the developer with an object, commonly referred to as res. For example, Exampleapp.get('/test', (req, res) => {    // use req and res here })The res object basically refers to the response that'll be sent out as part of this API call.The res.send function sets the content type to text/Html which means that the client will now treat it as text. It then returns the response to the client.The res.json function on the other handsets the content-type header to application/JSON so that the client treats the response string ... Read More

How to clone a js object except for one key in javascript?

Aman Kumar
Updated on 19-Dec-2022 12:21:28

1K+ Views

In computer programming, cloning refers to an object copied by a method or copy factory function often called clone and copy. The easiest way to clone an object except for one key would be to clone the whole object and then remove the property that is not required. Cloning, however, can be of 2 types − Deep Clone Shallow Clone Shallow Clone The shallow clone copies as little as possible. For example, a shallow copy of a collection might be a copy of the collection’s structure, not its elements. With a shallow copy, two collections now share individual ... Read More

How to prevent moment.js from loading locales with webpack?

Ayush Gupta
Updated on 02-Dec-2019 06:57:27

452 Views

A local file is a .json file that contains a set of translations for the text strings used in a theme template file. A separate local file is used for every language.When you require moment.js in your code and pack it with webpack, the bundle size becomes huge because it includes all locale files.You can remove all locale files using the IgnorePlugin. For example, Exampleconst webpack = require('webpack'); module.exports = {    plugins: [       // Ignore all locale files of moment.js       new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),    ], }; // load specific locales in your code. ... Read More

Jasmine.js comparing arrays

Ayush Gupta
Updated on 02-Dec-2019 06:55:34

1K+ Views

Arrays can be compared in 2 ways −They refer to the same array object in memory.They may refer to different objects but their contents are all equal.For case 1, jasmine provides the toBe method. This checks for reference. For example, Exampledescribe("Array Equality", () => {    it("should check for array reference equility", () => {       let arr = [1, 2, 3];       let arr2 = arr       // Runs successfully       expect(arr).toBe(arr2);       // Fails as references are not equal       expect(arr).toBe([1, 2, 3]);    }); });OutputThis ... Read More

Converting a string to a date in JavaScript

Aman Kumar
Updated on 19-Dec-2022 12:11:06

4K+ Views

In this article, we are going to discuss how to convert a string value to a Date object in JavaScript. There are two ways to achieve this − Using the constructor of the Date class − This constructor accepts a string value representing the date value, converts it into a Date object, and returns the result. Using the Date.parse() method − Same as the Date constructor this method accepts a string value parses and returns the date value in the form of milliseconds. Let us see these solutions with examples − Using the Date() constructor The most commonly used way ... Read More

Inserting string at position x of another string using Javascript

Aman Kumar
Updated on 19-Dec-2022 12:07:26

928 Views

In this article, you are going to learn how to insert a string at a position in another string. JavaScript does not give a direct way to achieve this. For this, we can use the slice method. The slice method extracts a section of a string and returns a new string. In addition to the slice() method we can also use the methods join() and substr(). Using the slice() Method of the String class This slice() method gets parts of the string and returns the extracted part from the string and makes a new string. Following is the syntax ... Read More

How to Clear the JavaScript Console in Google Chrome

Aman Kumar
Updated on 19-Dec-2022 12:05:07

16K+ Views

In this article we are going to learn how to clear the JavaScript Console in Google Chrome. We have various methods to clear the console; but our question is why do we need to clear the console? Sometimes we have lots of commands and logs printed in any browser console which makes it look substandard and packed with output. So, we need to clear the console. And there are multiple ways to clear the console of google chrome. Using the console.clear() method This method will clear the console and display the message on the console that the console was clear. ... Read More

Dynamically creating keys in JavaScript associative array

Aman Kumar
Updated on 19-Dec-2022 12:01:59

4K+ Views

In this article, we are going to discuss how to create keys in a JavaScript associative array dynamically. Associative arrays are dynamic objects that are user defined as needed. When you assign values to keys in a variable of types array, then the array is transformed into an object, and it loses the attributes and methods or array. i.e. the length attributes have no effect because the variables are no longer of the type array. JavaScript associative arrays are same as any other literals. You can add keys to these using the square brackets notation you can add keys to ... Read More

Why Ember.js is the best javascript frame work?

Aman Kumar
Updated on 19-Dec-2022 11:58:06

241 Views

Ember.js is the best framework. The user of Ember.js is increasing day by day. The popularity of ember.js is 6.3% of JavaScript developers currently using ember. Its popularity has stood over the last two years. Ranked 4th most popular JavaScript framework. Ember.js is an MVVM (Model−view−view−model) model framework for complex and multiple-page applications and it is an open-source framework. It is a very redefined control system that helps you to integrate with the new version without any problems. Ember.js is a free javascript client-side framework it is used for developing web applications. It is providing the complete solution which contains ... Read More

Advertisements