I know it's an old question but I thought I'd answer as I was looking for the exact same solution and hopefully help some others out.
This solved it for me how-to-get-the-tempdata-in-javascript
Essentially, Your syntax is missing parenthesis
//Your code
alert('@TempData["AlertMessage"]');
// Correct code
alert('@(TempData["AlertMessage"])');
The parenthesis after the @
Hope this helps the next searcher like me.