JavaScript is a versatile and powerful language, but with great power comes great responsibility. One feature that you should handle with care is the eval()
function. In this concise guide, we’ll explore why it’s best to avoid eval()
and seek alternative solutions.
Tag: code (Page 1 of 3)
In the world of JavaScript, cleaner and more readable code is a prized possession. Object shorthand is a handy technique that can help you achieve just that. In this concise guide, we’ll explore what object shorthand is and how it can simplify your code.
Continue readingJavaScript offers powerful tools for working with arrays – map
, filter
, and reduce
. In this concise guide, we’ll uncover the magic of these functions and how they can level up your data manipulation game.
Are you ready to unlock a magical feature in JavaScript that can make your code more elegant and readable? Say hello to “Destructuring”! In this beginner-friendly guide, we’ll explore what destructuring is and how it can simplify your code.
Continue readingOne of Python’s standout features is its ability to perform complex tasks with simplicity and readability. One area where this shines is dictionary comprehension with conditional expressions. In this blog post, we’ll delve into this technique, exploring how it can streamline your code and enhance your Python programming skills.
Continue readingPython’s implicit line continuation is a powerful feature that allows you to break long lines of code without the need for backslashes or explicit continuation characters.
By enclosing expressions within parentheses, brackets, or curly braces, Python recognizes the continuation and treats the code as a single logical line.
In this article, we will explore implicit line continuation and provide code examples to illustrate its usage and benefits.
Continue readingOne way to improve the concision of your Python code is by using the ternary operator, a handy tool that allows you to condense an if-else statement into a single line of code.
Let us jump straight into it.
Imagine that you have the following comparison:
x = 5
y = 10
if x > y:
result = x
else:
result = y
You can then write it in a single line:
result = x if x > y else y
Continue reading
Python is known to be the language that gives you the ability to write a few lines of code that can do a lot of things.
Just a few Python lines can be enough to pull data from an API that you can then also visualize in a fancy diagram.
Or even a single line of code can be enough for you to do plenty of things.
There are a lot of Python one-liners online that you can find.
Continue readingIt’s inevitable that you need to work with lists in your day-to-day job.
A list of users. A list of store items. A list of objects.
One potentially rare task that you may need to do is get a random element in a list.
Continue readingFinding the number of days is usually quite popular and well documentated. There can also be cases when we need to find a difference in terms of weeks or even months.
If you want to find the difference in months between 2 dates, then here is a quick Python script to help you do that.
For example, between October 2018 and November 2021, there isn’t just a month difference. There is also a difference in years as well.
Let us assume that we don’t have dates prepared yet, so we initialize 2 variables with 2 dates.
After that, we find the difference in terms of number of months in between.
We should keep in mind that we may have dates that are in different years, and it’s not enough to think that we can just subtract two dates. Because of that, we need to also find the difference in years.
Here is the entire script:
You may get the impression that we may have a negative number in the end, since we are subtracting the number representing a month from another month.
To illustrate, let us assume that we have the following:
Now, since we are subtracting the number of months from the first date and 3 < 12, we will indeed get -9. However, we also have in the end a difference in years that is equal 1, since 2021 – 2020 = 1.
Hence, we will add 12 to -9, and in the end we get the result equal to 3:
That’s pretty much it.
I hope you find this useful.