Tag Archives: Memeriksa

Program JavaScript untuk Memeriksa apakah suatu Angka Float atau Integer

Contoh 1: Menggunakan Number.isInteger() // program to check if a number is a float or integer value function checkNumber(x) { // check if the passed value is a number if(typeof x == ‘number’ && !isNaN(x)){ // check if it is integer if (Number.isInteger(x)) { console.log(`${x} is integer.`); } else { console.log(`${x} is a float value.`); […]

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 […]

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 […]

Program JavaScript untuk Memeriksa apakah Suatu Objek adalah Array

Contoh: Periksa Array Menggunakan Array.isArray() // program to check if an object is an array function checkObject(arr) { // check if arr is array const result = Array.isArray(arr); if(result) { console.log(`[${arr}] is an array.`); } else { console.log(`${arr} is not an array.`); } } const array = [1, 2, 3]; // call the function checkObject(array); […]

Program JavaScript untuk Memeriksa apakah Array Berisi Nilai Tertentu

Contoh 1: Periksa Array Menggunakan include() // program to check if an array contains a specified value const array = [‘you’, ‘will’, ‘learn’, ‘javascript’]; const hasValue = array.includes(‘javascript’); // check the condition if(hasValue) { console.log(‘Array contains a value.’); } else { console.log(‘Array does not contain a value.’); } Keluaran Array contains a value. Pada program […]

Program JavaScript untuk Memeriksa Tahun Kabisat

Satu tahun adalah tahun kabisat jika kondisi berikut dipenuhi: Tahun adalah kelipatan dari 400. Tahun adalah kelipatan dari 4 dan bukan kelipatan 100. Contoh 1: Periksa Tahun Kabisat Menggunakan if…else // program to check leap year function checkLeapYear(year) { //three conditions to find out the leap year if ((0 == year % 4) && (0 […]

Program JavaScript untuk Memeriksa Apakah String Berisi Substring

Contoh 1: Periksa String dengan include() // program to check if a string contains a substring // take input const str = prompt(‘Enter a string:’); const checkString = prompt(‘Enter a string that you want to check:’); // check if string contains a substring if(str.includes(checkString)) { console.log(`The string contains ${checkString}`); } else { console.log(`The string does […]

Program JavaScript untuk Memeriksa apakah sebuah String Dimulai Dengan String Lain

Contoh 1: Menggunakan startWith() // program to check if a string starts with another string const string = ‘hello world’; const toCheckString = ‘he’; if(string.startsWith(toCheckString)) { console.warn(‘The string starts with “he”.’); } else { console.warn(`The string does not starts with “he”.`); } Keluaran The string starts with “he”. Pada program di atas, startsWith() metode yang […]

Program JavaScript untuk Memeriksa Apakah Ada Kunci dalam Objek

Contoh 1: Periksa apakah Ada Kunci di Objek Menggunakan di Operator // program to check if a key exists const person = { id: 1, name: ‘John’, age: 23 } // check if key exists const hasKey = ‘name’ in person; if(hasKey) { console.log(‘The key exists.’); } else { console.log(‘The key does not exist.’); } […]

Program JavaScript untuk Memeriksa Apakah String Dimulai dan Diakhiri Dengan Karakter Tertentu

Contoh 1: Periksa String Menggunakan Metode Bawaan // program to check if a string starts with ‘S’ and ends with ‘G’ function checkString(str) { // check if the string starts with S and ends with G if(str.startsWith(‘S’) && str.endsWith(‘G’)) { console.log(‘The string starts with S and ends with G’); } else if(str.startsWith(‘S’)) { console.log(‘The string […]