Can I use standard inbuilt Javascript functions(better lack of words using standard functions) in my client side controller?

I did a search on standard javascript functions and got this as the top link:

https://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm

I started playing with some functions and all these functions work as expected.

1) Date

var date_v = Date();
component.set("v.date_val",date_v);

2) indexof

var index = str1.indexOf( "one" );
component.set("v.indexval",index);

Does this mean that I can use any JS function in the controller?

Please clarify

  • To be pedantic, you should be using var date_v = new Date() (don't forget the new). – sfdcfox May 18 '17 at 3:13
up vote 4 down vote accepted

You can use most of the standard JavaScript functions. There are a few that are not implemented for security reasons. You can see what's available and what's not in the Locker Service API. Several notable examples of things you can't use include ServiceWorker, ShadowRoot, MutationObserver, and a few others. For the day-to-day features that we typically use, like Array, Math, and Date, you'll find that all of those are available as usual.

  • Thanks for providing link to locker server api doc, this will be handy guide on checking the supported functions. – Swathi May 18 '17 at 16:42

Yes you can use any of these functions in lightning. JavaScript is a very mature browser language that has good functions for lot of data manipulations we do.

Always remember that through lightning we manipulate data and it takes care of re-rendering the UI in 99% of the cases(1% left for any custom rendering you may want to do).

You can use all array/object manipulation functions from the below link to do your day to day functions. It also serves as good guide for beginners to use some of the inbuilt functions in javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.