1function dec2bin(dec){
2 return (dec >>> 0).toString(2);
3}
4
5dec2bin(1); // 1
6dec2bin(-1); // 11111111111111111111111111111111
7dec2bin(256); // 100000000
8dec2bin(-256); // 11111111111111111111111100000000
1// string to binary
2parseInt(num.toString(2));
3
4// binary to string
5parseInt(num.toString(2), 2);
1/* From decimal to binary (similar fo rother base) */
2const num = 7;
3num.toString(2); // output: "111"
4
5/* From binary (similar fo rother base) to decimal */
6parseInt("111", 2); // output: 7