I just want to increase my core javascript knowledge.
Sometimes I see this statement but I don’t know what it does:
var var1 = var1 || [];
What does it means and/or what’s it for, and how do you use it?
Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The logical operators in JavaScript actually evaluate to one of the two objects. When you use
a || bit evaluates tobifais false, or toaifais true. Thusa || []will beaifais any value that is true, or[]ifais any value that is false.It’s much more obvious to use
if (!a) { a = [] };