0

I'm pretty new to javascript and i have following problem:

in an alert popup i'm getting back a variable with 2 values which are separated by ",".

alert example: 1,2

now i want to get this values separated so i get 2 different variables

value 1 = 1

value 2 = 2

when there is no value alert example: ,2 then it should look like this: value 1 = 0 value 2 = 2

0

1 Answer 1

2

Use split which will split the string into array using mentioned separator

expr1 || expr2 => Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

var data = ',2';
var splitted = data.split(',');
alert(splitted[0] || 0);
alert(splitted[1] || 0);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.