Learn J with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
J Counter and Theme Toggle
count =: 0
isDark =: 0
updateUI =: 3 : 0
'Counter: ', ": count
'Theme: ', (if. isDark do. 'Dark' else. 'Light')
)
increment =: 3 : 0
count = count + 1
updateUI ''
)
decrement =: 3 : 0
count = count - 1
updateUI ''
)
reset =: 3 : 0
count = 0
updateUI ''
)
toggleTheme =: 3 : 0
isDark = 1 - isDark
updateUI ''
)
'Simulate actions'
updateUI ''
increment ''
increment ''
toggleTheme ''
decrement ''
reset ''
Demonstrates a simple counter with theme toggling using J variables and verbs.
2
J Fibonacci Sequence
fib =: 0 1
for_i. 8 do. fib =: fib , (+/@:1) fib ; end.
fib
Generates first 10 Fibonacci numbers using J array operations.
4
J Prime Checker
isPrime =: 3 : 0
n = y
n > 1 &. >./ n | i.1 + n - 2
)
Checks if a number is prime using J modulo and all/any verbs.
7
J Multiplication Table
n =: 5
(n * i.10) +: 'x ' , n
Generates multiplication table for a number using J.
8
J Celsius to Fahrenheit
c =: 25
f =: c * 9 % 5 + 32
f
Converts Celsius to Fahrenheit using J expressions.
9
J Simple Alarm Simulation
temp =: 80
thresh =: 75
if. temp > thresh do. 'Alarm: Temperature Too High!' else. 'Temperature Normal'
Simulates an alarm if temperature threshold is exceeded.
10
J Random Walk Simulation
steps =: 10
pos =: 0
for_i. steps do. pos = pos + (0 < ?2) * 2 - 1 ; pos end.
Simulates a 1D random walk using J random verbs.