Javascript methods

Created By: chatGPT

JavaScript provides a plethora of built-in methods that can be used to perform a variety of operations. Here are a few commonly used methods: String methods, Array methods, and Object methods.String methods are essential for working with text data. Some of the most commonly used string methods include:
  • toUpperCase(): Converts a string to uppercase.
const text = 'hello';
const upperText = text.toUpperCase();
console.log(upperText); // Output: HELLO
  • toLowerCase(): Converts a string to lowercase.
const text = 'HELLO';
const lowerText = text.toLowerCase();
console.log(lowerText); // Output: hello
  • trim(): Removes whitespace from both ends of a string.
const text = '  hello  ';
const trimmedText = text.trim();
console.log(trimmedText); // Output: 'hello'
Array methods allow you to manipulate arrays effectively. Key array methods include:
  • push(): Adds one or more elements to the end of an array.
const arr = [1, 2, 3];
arr.push(4);
console.log(arr); // Output: [1, 2, 3, 4]
  • pop(): Removes the last element from an array.
const arr = [1, 2, 3, 4];
const lastElement = arr.pop();
console.log(lastElement); // Output: 4
  • map(): Creates a new array with the results of calling a provided function on every element in the calling array.
const arr = [1, 2, 3];
const doubled = arr.map(x => x * 2);
console.log(doubled); // Output: [2, 4, 6]
Object methods are used to manipulate objects in JavaScript. Here are a couple of important ones:
  • Object.keys(): Returns an array of a given object's property names.
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // Output: ['a', 'b', 'c']
  • Object.values(): Returns an array of a given object's property values.
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // Output: [1, 2, 3]
Understanding these core methods will greatly enhance your ability to write efficient JavaScript code and manipulate data effectively.
Introduction And SetupVariablesData TypesMethodsControl FlowFunctionsOperatorsData StructuresCommentsSyntaxEvent HandlingError HandlingPrimitive Data TypesReference Data TypesConstLetVarStringNumberBooleanNullUndefinedSymbolObjectArrayTemplate LiteralsArrow FunctionsRegular FunctionsCallApplyBindAsync FunctionsPromisesCallback FunctionsIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor EachBreak StatementContinue StatementTry CatchThrowFinallyScopeClosureThis KeywordPrototypesInheritanceConstructor FunctionsClass SyntaxStatic MethodsGettersSettersSpread OperatorRest OperatorDestructuring AssignmentMap MethodFilter MethodReduce MethodEvent ListenersDom ManipulationInnerHTMLQuerySelectorAddEventListenerPreventDefaultStopPropagationTimeoutsIntervalsJsonLocal StorageSession StorageRegexMath ObjectDate ObjectString MethodsArray MethodsObject MethodsDeep CloningShallow CloningCallback HellPromise ChainingAsync AwaitMap Vs ForEachFilter Vs ReduceNullish Coalescing OperatorOptional ChainingTypeof OperatorInstanceof OperatorStrict EqualityLoose Equality