1568187475
Destructuring applies to arrays and objects. It consists in breaking down complex structures (array, object) to extract the values we are interested in. Those manipulations make the code simpler and easier to read.
let [ valeur1, valeur2 ] = tableau
let { valeur1, valeur2 } = objet
The syntax is different wether it is an array or an object.
A concrete example of breaking arrays down is when you want to get values from an array.
let args = ['fruits', 'pomme', 'poire', 'abricot']
let type = args[0]
let firstEl = args[1]
let secondEl = args[2]
let thirdEl = args[3]
Without destructuring, here is how we can get the values from an array to use them.
let args = ['fruits', 'pomme', 'poire', 'abricot']
let [type, firstEl, secondEl, thirdEl] = args
Same thing with destructuring.
The two bits of code are similar, the result is the same. In each case, the variable type
, firstEl
, secondEl
, thirdEl
are created.
If we stop here, this syntax has not a great interest except sparing us three lines of code. But destructuring has a lot more in store that will make you time efficient.
We will not compare with examples without destructuring each time but try to think of how you would have code the same feature without destructuring.
Example : If I only want to get the type and the second element back.
let args = ['fruits', 'pomme', 'poire', 'abricot']
let [type,, secondEl] = args
You just have to add a coma between args[0]
and args[2]
without specifying the variable name of args[1]
.
Example : If args
is empty, my variables type
and secondEl
will be created but will have undefined
as value. To be able to continue working with it, I would have to test my variables. But I can also assign them default values.
let args = []
let [type = 'legume', firstEl = 'salade'] = args
As args
is empty, type
will have “légume” as value and firstEl
will have “salade” as value.
Example : If I want to get back to the type of my elements in a variable, but it would be easier for me to have the list of elements in an array to loop on.
let args = ['fruits', 'pomme', 'poire', 'abricot']
let [type, ...elements] = args
Here type
will have “fruits” as value and elements
will have ['pomme', 'poire', 'abricot']
.
The syntax here is quite particular because it uses what we call a rest paramater
You can also use destructuring in case you would not think about it usually :
Example : If I want to swap the values of two variables.
let valA = 'coucou'
let valB = 'byebye'
[valA, valB] = [valB, valA]
Logically valA
will have “byebye” as a value and valB
will have “coucou”.
Example : Destructuring can be used to work with functions that return arrays.
let [max, min] = getMaxAndMin([3, 34, 2, 5, 6, 22, 33, 1])
The interest here is the readability of the code. Of course, you will need to be sure of the order of the returned values by the used function.
Example : It is possible to use destructuring as function parameter.
function firstIsTrue([ first = false ]) {
return first === true
}
firstIsTrue()
take an array in parameter. Only the first value will be transmitted to the function and if the array is empty : first
will have false
as value.
For the function user, everything is transparent.
Destructuring objects is quite similar to arrays with a few specificities. We will cover that quickly to stop on more interesting notions.
Example : Retrieval of one of our keys
let objet = {
slug: 'test'
title: 'Ceci est un test'
content: '...'
visible: true
}
let { title } = objet
Standard example where we only want the title. You will note that we use brackets instead of square brackets when it is an object.
Example : Default value
let objet = {
slug: 'test'
title: 'Ceci est un test'
content: '...'
visible: true
}
let { visible = false } = objet
You only want visibility and a false
by default.
Example : Using an other name for a key
let objet = {
slug: 'test'
title: 'Ceci est un test'
content: '...'
visible: true
}
let { content: article } = objet
Here, we only retrieve the key content
that we are stocking in the article
variable.
The real syntax of destructuring is the one used in this example. In other examples, we used a tip from ES2015 that prevent us from writing the same thing twice when possible. Just remind that { visible: visible }
is equal{ visible }
.
Example : Accumulation of default value and name changing
let objet = {
slug: 'test'
title: 'Ceci est un test'
content: '...'
visible: true
}
let { visible: isVisible = false } = objet
We retrieve the key visible
that we are stocking in the variable isVisible
. This variable will be by default false
.
Example : Using destructuring as function parameter
It is the case in which I use the most destructuring. It saves so much time.
You probably already wrote some function with an options
parameter that gather all your options in an object.Think about all that wasted time and that number of lines you had to write to test this parameter before even coding business logic.
function test(id, { maxLength = 10, current = 0 } = {})
As for destructuring an array, using it in paramaters is absolutely transparent for users but it changes your whole life.
maxLength
and current
will never be undefined
, will always have a value, either the users or the default one. It will lighten your code as you will not be doing options.maxLength
anymore but just maxLength
as if it was a simple parameter as id
.
Only the attributes that you have declared in destructuring will be transmitted to your function. The user can add 15 000 other attributes to options
, they will not be transmitted. It adds a bit of security to your function too.
I really hope I convinced you to try it. Destructuring is, for me, one of the biggest ES2015 new feature.
A powerful function to use without moderation to make your code simpler, clearer, beautiful, cleaner… And you know how I like clean code.
#javascript #web-development
1606912089
#how to build a simple calculator in javascript #how to create simple calculator using javascript #javascript calculator tutorial #javascript birthday calculator #calculator using javascript and html
1599553500
Destructuring is a convenient way of extracting multiple values from data stored in objects and Arrays. ES6 introduced some significant improvements to the language, including the de-structuring of Objects and Arrays. Honestly speaking, these are my favorite edition in the JavaScript standard syntax.
Let us take a look if we want to extract data from an array in es5.
Here, we are doing something quite boring stuff and repeated the same thing again and again. But ES6 destructuring assignment makes it easier.
We can also do this with the same result,
we can also declare variables before Assignment,
Assign variables a default value:
#destructuring #array-destructuring #javascript-destructuring #es6-destructuring #es6
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, ‘document.write‘ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript
1616670795
It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.
Does your business need an interactive website or app?
Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
Get your business app with JavaScript
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers
1620729846
Can you use WordPress for anything other than blogging? To your surprise, yes. WordPress is more than just a blogging tool, and it has helped thousands of websites and web applications to thrive. The use of WordPress powers around 40% of online projects, and today in our blog, we would visit some amazing uses of WordPress other than blogging.
What Is The Use Of WordPress?
WordPress is the most popular website platform in the world. It is the first choice of businesses that want to set a feature-rich and dynamic Content Management System. So, if you ask what WordPress is used for, the answer is – everything. It is a super-flexible, feature-rich and secure platform that offers everything to build unique websites and applications. Let’s start knowing them:
1. Multiple Websites Under A Single Installation
WordPress Multisite allows you to develop multiple sites from a single WordPress installation. You can download WordPress and start building websites you want to launch under a single server. Literally speaking, you can handle hundreds of sites from one single dashboard, which now needs applause.
It is a highly efficient platform that allows you to easily run several websites under the same login credentials. One of the best things about WordPress is the themes it has to offer. You can simply download them and plugin for various sites and save space on sites without losing their speed.
2. WordPress Social Network
WordPress can be used for high-end projects such as Social Media Network. If you don’t have the money and patience to hire a coder and invest months in building a feature-rich social media site, go for WordPress. It is one of the most amazing uses of WordPress. Its stunning CMS is unbeatable. And you can build sites as good as Facebook or Reddit etc. It can just make the process a lot easier.
To set up a social media network, you would have to download a WordPress Plugin called BuddyPress. It would allow you to connect a community page with ease and would provide all the necessary features of a community or social media. It has direct messaging, activity stream, user groups, extended profiles, and so much more. You just have to download and configure it.
If BuddyPress doesn’t meet all your needs, don’t give up on your dreams. You can try out WP Symposium or PeepSo. There are also several themes you can use to build a social network.
3. Create A Forum For Your Brand’s Community
Communities are very important for your business. They help you stay in constant connection with your users and consumers. And allow you to turn them into a loyal customer base. Meanwhile, there are many good technologies that can be used for building a community page – the good old WordPress is still the best.
It is the best community development technology. If you want to build your online community, you need to consider all the amazing features you get with WordPress. Plugins such as BB Press is an open-source, template-driven PHP/ MySQL forum software. It is very simple and doesn’t hamper the experience of the website.
Other tools such as wpFoRo and Asgaros Forum are equally good for creating a community blog. They are lightweight tools that are easy to manage and integrate with your WordPress site easily. However, there is only one tiny problem; you need to have some technical knowledge to build a WordPress Community blog page.
4. Shortcodes
Since we gave you a problem in the previous section, we would also give you a perfect solution for it. You might not know to code, but you have shortcodes. Shortcodes help you execute functions without having to code. It is an easy way to build an amazing website, add new features, customize plugins easily. They are short lines of code, and rather than memorizing multiple lines; you can have zero technical knowledge and start building a feature-rich website or application.
There are also plugins like Shortcoder, Shortcodes Ultimate, and the Basics available on WordPress that can be used, and you would not even have to remember the shortcodes.
5. Build Online Stores
If you still think about why to use WordPress, use it to build an online store. You can start selling your goods online and start selling. It is an affordable technology that helps you build a feature-rich eCommerce store with WordPress.
WooCommerce is an extension of WordPress and is one of the most used eCommerce solutions. WooCommerce holds a 28% share of the global market and is one of the best ways to set up an online store. It allows you to build user-friendly and professional online stores and has thousands of free and paid extensions. Moreover as an open-source platform, and you don’t have to pay for the license.
Apart from WooCommerce, there are Easy Digital Downloads, iThemes Exchange, Shopify eCommerce plugin, and so much more available.
6. Security Features
WordPress takes security very seriously. It offers tons of external solutions that help you in safeguarding your WordPress site. While there is no way to ensure 100% security, it provides regular updates with security patches and provides several plugins to help with backups, two-factor authorization, and more.
By choosing hosting providers like WP Engine, you can improve the security of the website. It helps in threat detection, manage patching and updates, and internal security audits for the customers, and so much more.
#use of wordpress #use wordpress for business website #use wordpress for website #what is use of wordpress #why use wordpress #why use wordpress to build a website