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.`); […]
Tag Archives: Apakah
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 […]
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 […]
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); […]
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 […]
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 […]
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 […]
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.’); } […]
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 […]
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) { // […]
- 1
- 2