This question already has answers here:
JavaScript String concatenation behavior with null or undefined values
(7 answers)
Closed 6 years ago.
I have a global variable. The variable is equal to null. var a=null; Then I write a=a+"example" in console. But output is null example. Why is it so ?
There are three possibilities in javascript:
Option 1
var a;
a=a+"example";
output: "undefinedexample"
Option 2
var a=null;
a=a+"example";
output: "nullexample"
Option 3
var a="";
a=a+"example";
output: "example"
As per your Question you need to define third option. bcz in javascript null and "" both are different types.
For more ref JavaScript String concatenation behavior with null or undefined values
If You want to concatenate strings this way, You shouldn't assign null but an empty string. Value null will be changed to string 'null' in your code. Example:
var a = '';
for(var i=0; i<10; i++) {
a = a + 'abc';
}
As you are concatenating it with string it turning out to be string see this example it adopts the datatype you assign it to be for the initial value was null
if you concat with string it takes string type if number then with number type and so on
var s = null;
console.log(s+"example");
console.log(s+5);
console.log(s+17.5)
I don't know what u really expecting,according to your problem,i think you need to concatenate two string,if am correct you can use
var str1 = '';
var str2 = "example";
var res = str1.concat(str2);
instead of null you can use ''
Related
This question already has answers here:
Question mark and colon in JavaScript
(8 answers)
Closed 3 years ago.
I am wondering what this question mark symbol means in a function return statement in JS.
function getValue(val) {
return (val != null ? val.toString().replace(/,/g, '') : "");
}
Its a Conditional (Ternary) Operator:
Syntax:
variablename = (condition) ? value1:value2
Example:
var voteable = (age < 18) ? "Too young":"Old enough";
Explanation:
If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".
In this case "?" allow to write if ... else in one line, it's what we called ternary operator, refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
It's a way to choose a value conditionally based on another value.
Variables are 'truthy' in javascript, and so let's say you have a variable x, and you want to choose variable y based on if variable x is truthy or not
var y = x ? '1' : '2';
If x is truthy, y will be '1', otherwise '2'.
This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 7 years ago.
I am getting values as True and False from back-end. I am trying to convert those values as real Boolean values, But I am getting always true with my approach. What would be the correct way to do this?
here is my try:
var x = Boolean("False".toLowerCase());
console.log( x ); //giving true instead of false.
You can use this :
var str = "False";
var x = str.toLowerCase() == "false" ? false : true;
try this code.
var x = Boolean("true" == "False".toLowerCase());
Why replace function gives different results on the same string? The only difference I see is whether the string is in a variable or is a literal?
I am trying this in node REPL:
> "2015/05/21 23:59:59".replace(/^[0-9]/g, '')
'015/05/21 23:59:59'
> var time = "2015/05/21 23:59:59"
> time.replace(/[^0-9]/g, '')
'20150521235959'
This question already has answers here:
Javascript || operator
(5 answers)
What does the Javascript expression 'a = a || function() {...}' mean?
(4 answers)
Closed 9 years ago.
I usually use this code to see if the argument of a function is undefined or not
var = (typeof var != "undefined")? var : "othervalue";
but, other uses this boolean operator
var = var || "othervalue";
However, I have seen that if the value of checking and boolean false argument is not done correctly.
// assuming it is false
var = var || "othervalue"; // will be "OTHERVALUE"!!!
I need to know what exactly this operator in this context.
It returns the last expression that terminated the condition. It doesn't work the same as checking for typeof arg == "undefined", as any falsey value on the left will jump to the RHS.
|| operator will return the last expression, if first one is falsely:
var test = first || "second";
// error will be raised, because first variable is not defined
var first;
var test = first || "second";
// test = "second", because first is falsely
var first = "first";
var test = first || "second";
// test = "first"
I always use ternary operator with typeof expression, because it's a really common thing to forget to define some variable:
var test = 'undefined' != typeof(first) && first ? first : "second";
// test = first if first is defined and true
I believe this is a perl style selection for first true (pseudocode below)
eat_breakfast = null
eat_dinner = null
eat_lunch = "eating lunch"
myVal = eat_breakfast || eat_dinner || eat_lunch
print myVal
would print "eating lunch"
it will set myVal to the first non-null/non-false entity.
Probably a very basic question from a confused javascript noob...
Why do
var hasthisvalue = null;
if (hasthisvalue)
print("hasthisvalue hs value");
and
var hasthatvalue = "";
if (hasthatvalue)
print("hasthatvalue has value");
don't print anything, but if I combine these two
var combined = "hasthisvalue" + "hasthatvalue";
if (combined)
print ("combined has value");
it does?
Or more directly:
var combined = null + "";
if (combined)
print ("combined has value");
Why does "combined" has a value if I only add two variables that don't have values? What am I missing?
When you compare them separately, each converts to false in the if check. When you combine them, the null becomes the string "null", so their concatenation is the string "null", which does not convert to false
The first 2 examples are situations where the values are "falsy". These values are equal to false during a loose comparison:
null
undefined
blank string
boolean false
the number 0
NaN
Other values not in this list are "truthy" and are equal to true on a loose comparison.
The third case, you can try in the console. null+'' becomes a string: "null" and is therefore truthy.