JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions. The of this repo is to save my js programs. Basics of JavaScript. Beginner level.
Hello Javascript
- 1.1typeof: Returns the type.
console.log(typeof44);// numberconsole.log(typeof'something');// string console.log(typeoftrue);// booleanletnum=12;console.log(typeof(num));// number- 1.2toString: Returns the string representation of the number's value.
letnum=10;letn=num.toString();console.log(typeof(num));// numberconsole.log(typeof(n));// string- 1.3indexOf: Returns the first index at which a given element can be found in the array, or -1 if it is not present.
letstr="Hello world, welcome to the JS Universe.";console.log(str.indexOf("welcome"));// 13console.log(str.indexOf("wall"));// -1constfruits=['Orange','Pineapple','Apple','Melon'];console.log(fruits.indexOf('Melon'));// 3console.log(fruits.indexOf('klkljkh'));// -1- 1.4lastIndexOf: Returns the last index at which a given element can be found in the array, or -1 if it is not present.
constfruits=['Orange','Pineapple','Apple','Melon'];console.log(fruits.lastIndexOf('Melon'));// 3console.log(fruits.lastIndexOf('klkljkh'));// -1- 1.5length: Returns the number of characters or size in a string or array.
constfruits=['Orange','Pineapple','Apple','Melon'];console.log(fruits.length);// 4letstr="Hello world, welcome to the JS Universe.";console.log(str.length);// 40- 1.6Interview Qus: Tricky JavaScript Interview Questions and Answers
// remove duplicates form and arrayletarr=[1,2,2,3,4];console.log([...newSet(arr)]);// output test 1console.log(5<6<7);// true // 5 < 6 => true => true < 7 => true = 1 => 1 < 7 => trueconsole.log(7>6>5);// false // 7 > 6 => true => true > 5 => true = 1 => 1 > 5 = falseconsole.log(Math.max());// -Infinity lowest min number in jsconsole.log(Math.max(1,2,3,4));// 4// objletprofile={name: 'Lakshman'};// Object.freeze(profile); // freeze don't allow insert and updateObject.seal(profile);// freeze don't allow insert, remove but allow updateprofile.name='Gope';console.log(profile);// objletuser={name: 'Gope'};// age not allow any update but name doesObject.defineProperty(user,'age',{value: 4,writable: false})user.name='Lakshman'user.age=5;console.log(user);// TypeError: Cannot assign to read only property 'age' of object '#<Object>'- 1.7rename: Rename multiple files extentions at once by a command (Just for Win).
Get-ChildItem*.css|Rename-Item-NewName{$_.name-Replace'\.css','.scss'}- 1.8majority: Find Majority Element.
functionmajorityElement(arr){letcount=0,candidate=null;for(letnumofarr){if(count===0)candidate=num;count+=(num===candidate) ? 1 : -1;}returncandidate;}// Time complexity: O(n)// Space complexity: O(1)constarr=[3,2,3,4,3,1,6,6,7,8,6,9,6];console.log(majorityElement(arr));// Output: 6