The Page.ClientScript.RegisterHiddenField did not work for me and returned null.
You can do like this:
1-First solution:
define a hidden field and make sure you set runat=server
<input type="hidden" id="myhiddenField" runat="server" value="" />
then in your code behind assign any value you want to it
myhiddenField.Value= ViewState["name"].ToString();// or assign any value you want
in your javascript access it like this:
<script type="text/javascript">
function test()
{
var name = document.getElementById('myhiddenField').value;
alert(name)
}
</script>
2-Second solution
In case for some reasons you don't want to have a server input control you can put the hidden field in a literal tag
<asp:literal id="literal1" runat="server"><input type="hidden" id="myhiddenField" value="{0}"/></asp:literal>
and then assign a value to the literal in codebehind like this
literal1.Text = string.Format(literal1.Text, "somevalue"); // somevlue can be your ViewState value
then access it in javascript as usual
var name = document.getElementById('myhiddenField').value;
alert(name)
Note: if you are using update panels put the hiddenfields inside the contenttemplate tag of the updatepanel