Skip to content

Prevent string to become number in javascript

I have a string 100-10-0 in my MVC model. I have to pass this string to a javascript function on an HTML button click. My code is:

<button type="button" onclick="DeleteRow(@Model.BarCode)">Click Me!</button>

@Model.BarCode has the value “100-10-0”.

My javascript function is:

function DeleteRow(barCode)
    console.log(barCode) // Output is 90

Problem here is that when barcode comes to javascript method as parameter it is converted to number and the minus operator is applied on it, thats why it shows 90 on the console. How can i prevent this behavior ? i want 100-10-0 string and not 90. How can i tell javascript to consider it as string and not as number ?



<button type="button" onclick="DeleteRow('@Model.BarCode')">Click Me!</button>

This has solved the problem for me.

User contributions licensed under: CC BY-SA
7 People found this is helpful