JavaScript classes, introduced in ES6, are syntactical sugar over JavaScript prototype-based inheritance. Classes are in fact "special functions". You can define classes in JavaScript using the class keyword using the following syntax −class Person { // Constructor for this class constructor(name) { this.name = name; } // an instance method on this class displayName() { console.log(this.name) } }This is essentially equivalent to the following declaration −let Person = function(name) { this.name = name; } Person.prototype.displayName = function() { console.log(this.name) }This class can also be written as ... Read More
The \W metacharacter is used to find a non-word character.A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.Example// Containing any non word character: console.log(/\W/.test(" ")) console.log(/\W/.test(".!@#")) // Not containing non word characters: console.log(/\W/.test("a")) console.log(/\W/.test("B")) console.log(/\W/.test("9")) console.log(/\W/.test("_"))Outputtrue true false false false false
The \s metacharacter is used to find a whitespace character.A whitespace character can be −A space characterA tab characterA carriage return characterA new line characterA vertical tab characterA form feed characterExample// Not containing any white spaces: console.log(/\s/.test("1")) console.log(/\s/.test("test")) // Containing whitespace characters: console.log(/\s/.test(" ")) console.log(/\s/.test("a\tb")) console.log(/\s/.test("mn"))Outputfalse false true true true
In collections, one of the main functional requirement is to get the number of elements which are get stored in our collection so that one can decide whether to put more elements in it or not. Also, the number of elements is also required for iteration of collection.As we know Array and Arraylist both are used to store elements but both have different defined methods in order to know the number of elements stored in it.The array has a length method that provides the number of elements can be stored or in simple words capacity of the Array. Also, the ... Read More
For the difference between definition and declaration, one should consider their literal meaning first which includes Declare means to announce or proclaim while Define means to describe some entity.The following are the important differences between the Definition and the Declaration.Sr. No.KeyDeclarationDefinition1ConceptThe concept of declaration includes informing the compiler about properties of the variable such as its name, type of value it holds and the initial value if any it takes.While the definition is basically the actual implementation and memory location of function and about memory for the variable is allocated during the definition of the variable.2Exception in CBoth declaration and ... Read More
As we know java is a language that supports multi threading and on the basis of nature threads in java are classified into two types Daemon thread and User thread.The following are the important differences between Daemon Threads and User Threads.Sr. No.KeyDaemon ThreadsUser Threads1NatureDaemon thread is low in priority i.e JVM does not care much about these types of threads.User threads are recognized as high priority thread i.e. JVM will wait for any active user thread to get completed.2CPU availabilityIt is not guaranteed that Daemon thread always gets CPU usage whenever it requires due to its low priority.User thread always ... Read More
The Set class in JavaScript provides a clear method to remove all elements from a given set object. This method can be used as follows −Examplelet mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(1); mySet.add(3); mySet.add("a"); console.log(mySet) mySet.clear(); console.log(mySet)OutputSet { 1, 2, 3, 'a' } Set { }You can also individually remove the elements by iterating over them.Examplelet mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(1); mySet.add(3); mySet.add("a"); console.log(mySet) for(let i of mySet) { console.log(i) mySet.delete(i) } console.log(mySet)OutputSet { 1, 2, 3, 'a' } Set { }
A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.Ways to create a set in js−1. Using empty Set constructorlet mySet = new Set(); mySet.add(1); mySet.add(1); console.log(mySet)OutputSet { 1 } 2. Passing an iterable to the constructorThe set constructor accepts an iterable object(list, set, etc) using which it constructs a new set.Examplelet mySet ... Read More
A set is an abstract data type that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.You should use sets, whenever you want to store unique elements in a container for which the order doesn't matter and you mainly want to use it to check for membership of different objects. Sets are also useful when you want to perform operations ... Read More
Error or exception is something that refers to the interruption of code execution due to which the expected outcome could not be attained to the end-user.On the basis of the event when an error is generated or identified we can classify them as Compile time error and runtime error.The following are the important differences between Compile Time Errors and Runtime Errors.Sr. No.KeyCompile Time ErrorsRuntime Errors1ReferenceCompile-time errors are generally referred to the error corresponding to syntax or semantics.Runtime errors on the other hand refer to the error encountered during the execution of code at runtime.2DetectionCompile-time errors get detected by compiler at ... Read More