Category: JavaScript
How to install vue-router in Vue project
Vue Router is the official router for Vue.js. To install Vue Router type this in command prompt. After installing Vue Router, enter the script in main.js file Given is the Javascript sample of Vue Router
Breaking forEach loop in JavaScript
forEach loop in JavaScript cannot be broken by using break statement. Here are some ways to break a forEach loop: Using Array. length: arr=[1,2,3,4,5]; arr.forEach(function(value){ if(value == 2){ arr.length=0; } console.log(‘value=’, value); }); Using splice: arr=[1,2,3,4,5]; arr.forEach(function(value,index){ if(value == 2){ arr.splice(index+1, arr.length); }… Continue reading Breaking forEach loop in JavaScript
‘toReversed’ method in JavaScript
The method ‘toReversed()’ is added to JavaScript in ECMAScript 2023 (ES 13) version. The advantage of this method over ‘reverse()’ is that it doesn’t change the original array. It reverses the array and returns a new array. Below is the example code for it and the output: x=[1,2,3,4,5]; x.reverse(); console.log(‘The reverse method applied and the… Continue reading ‘toReversed’ method in JavaScript
Merge two arrays and removes all duplicates elements
JavaScript function that merges two arrays and removes all duplicate elements. Test data:var array1 = [1, 2, 3];var array2 = [2, 30, 1];// console.log(merge_array(array1, array2));// Output: [3, 2, 30, 1]
JavaScript Array Functions
Map, reduce, and filter are all array methods in JavaScript. Each one will iterate over an array and perform a transformation or computation. Each will return a new array based on the result of the function. Map The map() method is used to create a new array from an existing one, applying a function to each… Continue reading JavaScript Array Functions
Convert an SVG file to Vue file.
Consider this is an svg file and the code looks like this. Use <template> tag and insert the content inside in it as shown below. Then save the file with .vue extension
Install and use Pinia in Vue.
Pinia is a store library for Vue, it allows you to share a state across components/pages. To install pinia type this in command prompt. After installing pinia, enter the script in main.js file Given is the Javascript sample of pinia
Class basic syntax
Class basic syntax In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).Wikipedia In practice, we often need to create many objects of the same kind, like users, or goods or whatever. As we already know from the chapter Constructor, operator… Continue reading Class basic syntax
JavaScript Weird Behaviour
✍🏻 Notation // -> is used to show the result of an expression. For example: 1 + 1; // -> 2 // > means the result of console.log or another output. For example: console.log(“hello, world!”); // > hello, world! // is just a comment used for explanations. Example: // Assigning a function to foo constant const foo = function() {};… Continue reading JavaScript Weird Behaviour