Convert Strings to Numbers with Vanilla JavaScript

Ayush Gupta
Updated on 02-Dec-2019 05:41:54

224 Views

The parseInt function available in JavaScript has the following signature −SyntaxparseInt(string, radix);Where the parameters are the following −string − The value to parse. If this argument is not a string, then it is converted to one using the ToString method. Leading whitespace in this argument is ignored.radix − An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the string.So we can pass the string and the radix and convert any number with base from 2 to 36 to integer using this method. For example, Exampleconsole.log(parseInt("100", 10)) console.log(parseInt("10", 8)) console.log(parseInt("101", 2)) console.log(parseInt("2FF3", 16)) ... Read More

Jasmine JavaScript Testing: toBe vs toEqual

Ayush Gupta
Updated on 02-Dec-2019 05:39:23

842 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.ExampleFor case 1, jasmine provides the toBe method. This checks for reference. For example, describe("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

Are DOM Element Listeners Removed from Memory in JavaScript?

Ayush Gupta
Updated on 02-Dec-2019 05:35:14

3K+ Views

In modern browsers, if a DOM Element is removed, its listeners are also removed from memory in javascript.Note that this will happen ONLY if the element is reference-free. Or in other words, it doesn't have any reference and can be garbage collected. Only then its event listeners will be removed from memory.

Difference Between throw new Error and throw SomeObject in JavaScript

Ayush Gupta
Updated on 02-Dec-2019 05:34:13

575 Views

The difference between 'throw new Error' and 'throw someObject' in javascript is that throw new Error wraps the error passed to it in the following format −{    name: 'Error',    message: 'Whatever you pass in the constructor' }The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

Differences Between Deferreds, Promises, and Futures in JavaScript

Ayush Gupta
Updated on 02-Dec-2019 05:31:52

1K+ Views

Future is an old term that is same as promise.A promise represents a value that is not yet known. This can better be understood as a proxy for a value not necessarily known when the promise is created.A deferred represents work that is not yet finished. A deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so. This can also be thought of as a promise that'll always succeed only.A promise is a placeholder for a result which is initially unknown while a deferred represents the computation that results in the value.Read More

Object and Class in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:18:19

201 Views

There are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method.An object within Perl is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.A class within Perl is a package that contains the corresponding methods required to create and manipulate objects.A method within Perl is a subroutine, defined with the package. The first argument to ... Read More

Send Email with Attachment using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:15:51

2K+ Views

If you want to send an attachment in your email using Perl, then following script serves the purpose −#!/usr/bin/perl use MIME::Lite; $to = 'abcd@gmail.com'; $cc = 'efgh@mail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; $msg = MIME::Lite-=>new(    From => $from,    To => $to,    Cc => $cc,    Subject => $subject,    Type => 'multipart/mixed' ); # Add your text message. $msg->attach(    Type => 'text',    Data => $message ); # Specify your file as attachement. $msg->attach(Type => 'image/gif',    Path => '/tmp/logo.gif',    Filename => 'logo.gif', ... Read More

Sending an HTML Message Using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:14:02

666 Views

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html in the header part of the email as follows −#!/usr/bin/perl $to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; open(MAIL, "|/usr/sbin/sendmail -t"); # Email Header print MAIL "To: $to"; print MAIL "From: $from"; print MAIL "Subject: $subject"; print MAIL "Content-type: text/html"; # Email Body print MAIL $message; close(MAIL); print "Email Sent Successfully";

Send a Plain Message Using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:12:41

391 Views

If you are working on Linux/Unix machine then you can simply use sendmail utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct. This may be different for your Linux/Unix machine.#!/usr/bin/perl $to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; open(MAIL, "|/usr/sbin/sendmail -t"); # Email Header print MAIL "To: $to"; print MAIL "From: $from"; print MAIL "Subject: $subject"; # Email Body print MAIL $message; close(MAIL); ... Read More

The G Assertion in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:10:09

372 Views

The \G assertion in Perl allows you to continue searching from the point where the last match occurred. For example, in the following code, we have used \G so that we can search to the correct position and then extract some information, without having to create a more complex, single regular expression −Example Live Demo#!/usr/bin/perl $string = "The time is: 12:31:02 on 4/12/00"; $string =~ /:\s+/g; ($time) = ($string =~ /\G(\d+:\d+:\d+)/); $string =~ /.+\s+/g; ($date) = ($string =~ m{\G(\d+/\d+/\d+)}); print "Time: $time, Date: $date";When the above program is executed, it produces the following result −Time: 12:31:02, Date: 4/12/00The \G assertion is ... Read More

Advertisements