Program JavaScript untuk Memeriksa Apakah Variabel adalah Tipe Fungsi

Contoh 1: Menggunakan instanceof Operator // program to check if a variable is of function type function testVariable(variable) { if(variable instanceof Function) { console.log(‘The variable is of function type’); } else { console.log(‘The variable is not of function type’); } } const count = true; const x = function() { console.log(‘hello’) }; testVariable(count); testVariable(x); Keluaran […]

Read more »

Program JavaScript Untuk Memeriksa Apakah Variabel Tidak Terdefinisi atau null

Contoh 1: Centang undefined atau null // program to check if a variable is undefined or null function checkVariable(variable) { if(variable == null) { console.log(‘The variable is undefined or null’); } else { console.log(‘The variable is neither undefined nor null’); } } let newVariable; checkVariable(5); checkVariable(‘hello’); checkVariable(null); checkVariable(newVariable); Keluaran The variable is neither undefined nor […]

Read more »

Program JavaScript untuk Menukar Dua Variabel

Contoh 1: Menggunakan Variabel Sementara //JavaScript program to swap two variables //take input from the users let a = prompt(‘Enter the first variable: ‘); let b = prompt(‘Enter the second variable: ‘); //create a temporary variable let temp; //swap variables temp = a; a = b; b = temp; console.log(`The value of a after swapping: […]

Read more »