Hello Mani,
You can’t add the Twitter Cards markup using client-side JavaScript since the Twitterbot will fetch your page and analyze the markup on the server without rendering the pages.
However, you are more than welcome to dynamically populate the Twitter Cards tags on the server-side itself. If you’re using a programming language to serve your pages, you can definitely add the tags at this stage. Alternatively, if you’re using a blog engine or a CMS, you’ll likely find some plugins to add Twitter Cards for you.
Let us know if we can help further.
Thanks,
Romain