Skip to content

C and javascript comparison

1. Hello World

C JavaScript
printf("Hello, World!"); console.log("Hello, World!");

2. Variables

C JavaScript
int x = 5; let x = 5; or const x = 5;
float y = 3.14; let y = 3.14;
Must declare type Dynamic typing (no type needed)

3. Input/Output

C JavaScript (Node.js or browser)
scanf("%d", &x); prompt("Enter a number:")
printf("%d", x); console.log(x);

4. Functions

C JavaScript
```c ```js
int add(int a, int b) { function add(a, b) {
return a + b; return a + b;
} }
``` ```

5. Conditionals

C JavaScript
if (x > 0) { ... } if (x > 0) { ... }
Same syntax Same syntax

6. Loops

C JavaScript
for (int i=0; i<5; i++) for (let i=0; i<5; i++)
while (x < 10) while (x < 10)
do { ... } while (x < 10); do { ... } while (x < 10);

7. Arrays

C JavaScript
int nums[3] = {1,2,3}; let nums = [1, 2, 3];
Fixed size Dynamic size

8. Strings

C JavaScript
char str[20] = "Hello"; let str = "Hello";
Manual memory handling Strings are objects

9. Memory Management

C JavaScript
Manual (malloc, free) Automatic (Garbage Collected)

10. Comments

C JavaScript
// single line // single line
/* multi-line */ /* multi-line */

Bonus: Print Sum Example

C JavaScript
```c ```js
int a = 1, b = 2; let a = 1, b = 2;
printf("%d", a + b); console.log(a + b);
``` ```

Let me know if you want a printable version or cheat sheet image! 📄✨