Tag Archives: Apakah

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

Program JavaScript untuk Memeriksa Apakah String adalah Palindrome atau Bukan

Senar adalah palindrom jika dibaca sama dari depan atau belakang. Sebagai contoh, ayah membaca hal yang sama baik dari depan maupun belakang. Jadi kata ayah adalah palindrom. Demikian pula, Nyonya juga sebuah palindrom. Contoh 1: Periksa Palindrome Menggunakan for Loop // program to check if the string is palindrome or not function checkPalindrome(str) { // […]