Programming Articles - Page 2444 of 3366

Why do we need weakMaps in Javascript?

Abdul Rawoof
Updated on 26-Aug-2022 11:57:42

463 Views

WeakMap is a collection in JavaScript. This type of collection is used to store the data in the form of key-value pairs. In WeakMap, the key must definitely be an object and the values can be of any type. The difference between a Map and a WeakMap is, in weakmap key must be an object and the other difference is that a weakmap is like a blackbox where the keys can’t be retrieved. The value of the weakmap can be accessed only if the key is known which means the values in the weakmap are private. Additional data can be ... Read More

How to find operating system in the client machine using JavaScript?

Abdul Rawoof
Updated on 26-Aug-2022 11:58:35

1K+ Views

The type of operating system used in the client machine can be detected using some of the functions in JavaScript. The different functions are discussed below. Using navigator.appVersion This property will return the information about the browser and the operating system being used in the form of a string. Syntax The syntax for the navigator.appVersion is given below. navigator.appVersion Example 1 This example demonstrates the usage of navigator.appVersion to detect client OS − Click to get the operating system Operating System ... Read More

Disadvantages of using innerHTML in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 12:00:15

3K+ Views

HTML stands for Hyper Text Markup Language, through the HTML we can design a block of webpages. Html is a frontend markup language that is used to build the content of frontend pages. It means that we can build a structure of web pages. Through HTML we can design the content of any website. It means that we can create headings, buttons, paragraphs, headers, footers, links, etc for any website. Example let’s try to understand to implement a program − Basic of ... Read More

Instanceof operator in JavaScript

Abdul Rawoof
Updated on 26-Aug-2022 11:59:38

1K+ Views

The Instanceof operator in JavaScript checks for the type of object at the run time environment. The result it returns is of Boolean type i.e., if the given input object is same as the object for which it is being checked, then it returns “true” or else it returns “false”. Syntax The syntax of Instanceof operator is shown below − var myVar = nameOfObject instanceof typeOfObject Instanceof operator is used in JavaScript for a unique purpose. As in JavaScript some of the variables are declared without mentioning the type of variable or the data type. In C, C++, Java ... Read More

How do we check if an object is an array in Javascript?

Abdul Rawoof
Updated on 26-Aug-2022 11:59:58

308 Views

Array is a data type which can store multiple elements of similar data types. For example, if an array is declared as integer data type then it stores one or more elements of the integer data type. To check if the given object or to know the data type, we can use directly the typeof() method in JavaScript. Using typeof() method typeof() is a function which gives the type of the given object. The typeof() will return a string which is the data type of the given operand. The operand can be an object, a variable or a function. For ... Read More

Difference between shift() and pop() methods in Javascript

Ayush Gupta
Updated on 16-Sep-2019 07:03:11

533 Views

The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.The pop() method removes the last element from an array and returns that element. This method changes the length of the array.Examplelet fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; console.log(fruits.pop()) console.log(fruits2.shift()) console.log(fruits) console.log(fruits2)Outputkiwi apple [ 'apple', 'mango', 'orange' ]  [ 'mango', 'orange', 'kiwi' ] Note that both the original arrays were changed here.Shift is slower than pop because it also needs to shift all ... Read More

Can we throw an Unchecked Exception from a static block in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:57:15

2K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExceptions in static blockJust like any other method in Java when an exception occurs in static block you can handle it using try-catch ... Read More

How do you throw an Exception without breaking a for loop in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:54:22

9K+ Views

Whenever an exception occurred in a loop the control gets out of the loop, by handling the exception the statements after the catch block in the method will get executed. But, the loop breaks.Example Live Demopublic class ExceptionInLoop{    public static void sampleMethod(){       String str[] = {"Mango", "Apple", "Banana", "Grapes", "Oranges"};          try {             for(int i=0; i

Can we to override a catch block in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:46:56

1K+ Views

DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling method.When a piece of code in a method throws an exception (compile time) we must either handle it by wrapping it within the try-catch pair or, throw it (postpone) to the calling method using the throws keyword else a compile time error occurs.In the following Java example the code in ... Read More

How to get Exception log from a console and write it to external file in java?

Maruthi Krishna
Updated on 12-Sep-2019 08:39:04

1K+ Views

There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) and performing division between them. We are wrapping this code in try block with three catch blocks catching ArithmeticException, InputMismatchException and, ArrayIndexOutOfBoundsException. In each of them we are invoking the writeToFile() method.This method accepts an exception object, and appends it to a file using the write() method of the Files ... Read More

Advertisements