Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

short-circuit evaluation , use of || operator

/*
Truthy and Falsy Assignment:
Truthy and falsy evaluations open a world of short-hand possibilities!

Say you have a website and want to take a user’s username to make a 
personalized greeting. Sometimes, the user does not have an account, 
making the username variable falsy. The code below checks if 
username is defined and assigns a default string if it is not:
*/




let username = '';
let defaultName;
 
if (username) {
  defaultName = username;
} else {
  defaultName = 'Stranger';
}
 
console.log(defaultName); // Prints: Stranger
/*
If you combine your knowledge of logical operators you can use a SHORT-HAND
for the code above. In a boolean condition, JavaScript assigns the truthy 
value to a variable if you use the || operator in your assignment:
*/

let username = '';
let defaultName = username || 'Stranger';
 
console.log(defaultName); // Prints: Stranger

/*
Because || or statements check the left-hand condition first, the variable 
defaultName will be assigned the actual value of username if it is truthy, 
and it will be assigned the value of 'Stranger' 
if username is falsy. This concept is also referred to as short-circuit evaluation.
*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser asteroid movement 
Javascript :: javascript server side 
Javascript :: Setting Multiples Properties With Array 
Javascript :: convert json date to java date 
Javascript :: vimscript replace function 
Javascript :: string to date with ist javascript 
Javascript :: In Self Invoking Functions, the This Below Console.Logs The Created Object 
Javascript :: javascript check if a number starts with another number 
Javascript :: datatables data in one line 
Javascript :: map sord elo 
Javascript :: adding amplify in index.js react native 
Javascript :: repeated click onchange javascript 
Javascript :: 20 most common question in javascript 
Javascript :: convert .js to .ts 
Javascript :: square brackets javascript object key 
Javascript :: how to find default or the first server discord.js 
Javascript :: https://javascript.plainenglish.io/javascript-algorithms-valid-parentheses-leetcode-71c5b2f61077 
Javascript :: js filter out html 
Javascript :: geteliment by id in javascript 
Javascript :: slicer 
Javascript :: how to check if a div tag contains child 
Javascript :: xor two hex strings js 
Javascript :: hide and show button react js 
Javascript :: javascript invert binary tree 
Javascript :: hello world js 
Javascript :: 206. reverse linked list javascript 
Javascript :: for-of loop 
Javascript :: creating the find method javascript 
Javascript :: heroku 
Javascript :: eleventy filter newlines 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =